[ [[tutorial-en-001 | Intro]] | [[tutorial-en-002 | Begin]] |
[[tutorial-en-003 | if/else]] | [[tutorial-en-004 | Loops]] |
[[tutorial-en-005 | Arrays]] | [[tutorial-en-006 | Graphics]] |
[[tutorial-en-007 | Animation]] | [[tutorial-en-008 | Mouse]] |
[[tutorial-en-009 | Game]] | [[tutorial-en-010 | Real]] |
[[tutorial-en-011 | Methods]] | [[tutorial-en-012 | Class]] |
[[tutorial-en-013 | Class 2]] | [[tutorial-en-014 | Applet]] |
[[tutorial-en-015 | MouseClick]] | [[tutorial-en-016 | Thread]] |
[[tutorial-en-017 | Button]] ]
----
====== Java Classes and Constructors (continued) ======
As we learnt in our last lesson, a java class can be thought of an object
and copies of that class can be made and called separate names.
This is called ** instantiation **.
Constructors are used to instantiate members of a class. You can use
the default one supplied or write your own. When you
overwrite the default constructor the default one is no longer available.
===== Scope =====
Scope in java programming is the portion of a program within which
a variable exists. Whatever set of curly brackets a variables is declared
in then it cannot exist outside of those curly brackets. Try this:
public class TestScope {
public static void main(String[] args) {
private int myNum = 5;
// scope is inside the main method
System.out.println(myNum);
// what do you get?
}
}
Then put myNum inside a method outside of the main method. Its scope
is only the doSomething() method. It does not exist outside the curly
brackets in which it was created. When a variable goes out of scope
it is destroyed.
public class TestScope {
public static void main(String[] args) {
System.out.println(myNum);
// what do you get now?
}
public void doSomething() {
private int myNum = 5;
// scope is inside the getNumber method
}
}
===== Java Multiple Constructors =====
We can always use the default constructor of a class if it meets our needs.
It is automatically there when a class is declared. Here are some examples
to recap:
Circle redCircle = new Circle();
Circle blueCircle = new Circle();
Employee mechanic = new Employee();
Employee welder = new Employee();
As we saw last lesson, we can overwrite our default constructor with our own:
public class Circle {
int xcenter;
int ycenter;
int radius;
// our overwritten constructor
public Circle() {
radius = 1;
}
}
When the above constructor is used the radius has a value of 1.
If we wanted to make a Constructor that allowed us to give our own value
to its radius, for example, we could supply a constructor as follows:
public Circle(int _radius) {
radius = _radius;
}
And it would be called like so:
Circle smallCircle = new Circle(2);
// radius = 2
or
Circle bigCircle = new Circle(569);
// radius = 569
We could go even further and allow the users of our class to set
every variable of our Circle class by supplying a further Constructor
with 3 arguments:
public Circle(int _x, int _y, int _radius) {
xcenter = _x;
ycenter = _y;
radius = _radius;
}
Let us see what our class looks like with our added constructor now:
public class Circle {
int xcenter;
int ycenter;
int radius;
// our overwritten default constructor
public Circle() {
radius = 1;
}
// Constructor with 1 argument
public Circle(int _radius) {
radius = _radius;
}
// our constructor with 3 arguments
public Circle(int _x, int _y, int _radius) {
xcenter = _x;
ycenter = _y;
radius = _radius;
}
}
So now we have 3 choices when we instantiate our Circle class. We can go:
Circle defaultCircle = new Circle(); // radius is 1
or:
Circle uniCircle = new Circle(27); // radius is 27
or:
Circle customCircle = new Circle(100, 75, 14);
This custom Circle will be created at position 100:75, and have a radius of 14.
Of course, this Circle class would need set and get access methods
to be written, especially if you wanted to move them around or
change them someway.
=== Java Programming Exercise ===
Create your circle class using the above constructors and create all
the access methods as well (getx, setx, getRadius, setRadius, etc..).
Create a class called TestCircle with a main method to test all
your constructors and access methods.
What did you come up with?