Constructors

A constructor has the same name as the class and does not have any return value, not even void.

If you don't put in a constructor, Java uses the default constructor of the super-class of your class. Since all classes are subclasses of the Object class, this works fine for classes that do not extend other classes.
The only problem is if you have a superclass that does not have a default constructor. This is discussed in the subclasses page.

Objects are actually made by using the operator "new". However, the new operator requires that it be followed by a call to a constructor. So, you can never separate "new" from the constructor.
The name of the constructor provides the name of the class to instantiate.

The new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type, like: Point ptOne = new Point(23, 94);
Note: you can also assign the reference to any superclass of the constructor

You do not have to assign the object to a variable. You can use it as soon as it's made.
myLabel.setFont( new Font("Serif", Font.BOLD, 14)); This makes a Font object (bold, serif, 14pt) but doesn't assign it to a variable. It is used immediately in setting the font of the label. The Font object that you made cannot be accessed again, e.g. for other labels, since it is not assigned to a variable. (Presumably, it still lives inside the myLabel object.)
also: g.setColor(new Color(0,255,0,100));
This also common practice when using ArrayLists: movieCollection.add(new Movie("Gone with the Wind"));
Cool trick: You can also use the methods of objects without assigning the object to something: int areaOfRectangle = new Rectangle(100, 50).getArea(); This makes a rectangle of size 100x50, then calculates its area and assigns that to the int variable. Of course, this is a silly example, but it shows you how to use this.

* Something to avoid

You can have a method that has the same name as the class too!

/* Even though you can do this ... */
/* ... it is a VERY BAD IDEA to have methods with the same names as the constructor. It's too confusing. */

public class ConstrTest {

    public static void main(final String[] args) {
        ConstrTest();
        new ConstrTest();
        new ConstrTest("a parameter");
	
	//new Test2();  //if you make a Test2 object here (static main), then the Test2 class must be static
		
    }

    static void ConstrTest() {		//static method with same name as constructor
        System.out.println("Method");
    }

    ConstrTest() {
        System.out.println("Constructor");
        new Test2();		//make Test2 object
    }
	
    ConstrTest(String s) {
        System.out.println("Constructor: " + s);
    }

    class Test2 {  //inner classes only have to be static if they are called from a static method

	Test2() {
	   System.out.println("Constructor-Test2");
	   Test2();
	}
	 
	void Test2() {			//instance method with same name as constructor
	   System.out.println("Method-Test2");
	}
    }
}

Overloading constructors

Using "this" and private

Most of this is duplicated in the section on objects and instance variables. Please read that first, then continue here.

this
Assume that you have a complicated object that requires a lot of initialization.

double engineSize;
int engineNums;
String type;
double thrust;
double liftCapacity;

Rocket(String type, double thrust, double liftCapacity) { 
	this.thrust = thrust;
	this.type = type;
	this.liftCapacity = liftCapacity;
	
	//complex stuff here. Tons of formulas.
	engineSize = .....
	engineNums = engineSize * ...
}
Now perhaps you want to create a rocket with default parameters and only specify the liftCapacity. We don't want to have to copy all of the formulas from the main constructor, because what if we need to make a change? Then we'd have to make a change in all other constructors too.

A critical principle in programming is to organize things so that you write a specific set of instructions in only one place -- e.g. make it a function and everything that needs it can call that function.

We overload the constructor, just as we can overload functions:

Rocket(double liftCapacity) { 
	this("Saturn V", 20000.0, liftCapacity);	//we never have to change any calulations here
}
Here, this refers to any other constructor with the parameters specified. In this case, we are calling the constructor above, which has parameters of string, double, double.
this( ) can only ever be on the first line in a constructor. We can't put it further down.

private
You can prevent someone from using the default contructor to make an object by writing
private MyClass() {} //default constructor is private
The reason for doing this is if you want to force someone to make objects with certain required fields.