Class Object and Method
 
Constructors for Classes
 
All Java classes have constructors that are used to initialize a new object of that type.
A constructor has the same name as the class.
 
For example,
The name of the Stack class's constructor is Stack, the name of the Rectangle class's constructor is Rectangle, and the name of the Thread class's constructor is Thread. Stack defines a single constructor:
 
public Stack()
{
items = new Vector(10);
}
 
A constructor uses its arguments to initialize the new object's state. When creating an object, choose the constructor whose arguments best reflect how we want to initialize the new object.
When declaring constructors for our class, we use the following access specifiers in the constructor declaration to specify what other objects can create instances of our class:
 
 
private
No other class can instantiate our class. Our class may contain public class methods (sometimes called factory methods), and those methods can construct an object and return it, but no other classes can.
 
 
protected
Only subclasses of our class can create instances of it.
 
 
public
Any class can create an instance of our class.
 
 
package
Only classes within the same package as our class can construct an instance of it. Constructors provide a way to initialize a new object.