User Tools

Site Tools


lang:judo:english:tutorial-en-013

[ Intro | Begin | if/else | Loops | Arrays | Graphics | Animation | Mouse | Game | Real | Methods | Class | Class 2 | Applet | MouseClick | Thread | 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?

lang/judo/english/tutorial-en-013.txt ยท Last modified: 2007/02/08 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki