Studyon Minte9.com
Fundamentals (Docs)

Study

Constructors



Constructors

Constructor declarations look like method declarations - except that they use the name of the class 
and have no return type.

	public Bicycle(int startCadence, int startSpeed, int startGear) {
		gear = startGear;
		cadence = startCadence;
		speed = startSpeed;
	}

To create a new Bicycle object called myBike, a constructor is called by the new operator:

	Bicycle myBike = new Bicycle(30, 0, 8);

Although Bicycle only has one constructor, it could have others, including a no-argument constructor:
	
	public Bicycle() {
		gear = 1;
		cadence = 10;
		speed = 0;
	}
	Bicycle yourBike = new Bicycle(); 
	// invokes the no-argument constructor to create a new Bicycle object called yourBike.


Methods

Parameters refers to the list of variables in a method declaration. 
Arguments are the actual values that are passed in when the method is invoked. 


Parameter Types

In this example, the method creates a new Polygon object and initializes it from an array of Point objects 
(assume that Point is a class that represents an x, y coordinate)

	public Polygon polygonFrom(Point[] corners) {
	    // method body goes here
	}

The Java programming language doesn't let you pass methods into methods. 
But you can pass an object into a method and then invoke the object's methods.


Arbitrary Number of Arguments

You can use a construct called varargs to pass an arbitrary number of values to a method. 
To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), 
then a space, and the parameter name.

	public Polygon polygonFrom(Point... corners) {
		int numberOfSides = corners.length;
		// ...
	}

It's a shortcut to creating an array manually.
You can see that, inside the method, corners is treated like an array.
You will most commonly see varargs with the printing methods; for example, this printf method:

	public PrintStream printf(String format, Object... args) { 
		// 
	}
	System.out.printf("%s: %d, %s%n", name, idnum, address);
	// or like this
	System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, email);


Passing Primitive Data Type Arguments

Primitive arguments, such as an int or a double, are passed into methods by value. 
This means that any changes to the values of the parameters exist only within the scope of the method. 
When the method returns, the parameters are gone and any changes to them are lost.

	public static void main(String[] args) {
		int x = 3;
		passMethod(x); // invoke passMethod() with x as argument
		System.out.println("After invoking passMethod, x = " + x); // x = 3
	   
	}
	public static void passMethod(int p) {
		p = 10;
	}


Passing Reference Data Type Arguments

Reference data type parameters, such as objects, are also passed into methods by value. 
This means that when the method returns, the passed-in reference still references the same object as before. 
However, the values of the object's fields can be changed in the method, if they have the proper access level.
	
	public void moveCircle(Circle circle, int deltaX, int deltaY) {
	    circle.setX(circle.getX() + deltaX);
	    circle.setY(circle.getY() + deltaY);    
	    circle = new Circle(0, 0); // code to assign a new reference to circle
	}
	moveCircle(myCircle, 23, 56); // invoke method


http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html