• A typical Java program creates many objects, which as you know, interact by invoking methods.


  • Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network.


  • Once an object has completed the work for which it was created, its resources are recycled for use by other objects.


  • Below is a small program, called CreateObjectDemo, that creates objects: one Point object.


  •  
    
    public class Point {
        public int x = 0;
        public int y = 0;
    	// a constructor!
        public Point(int a, int b) {
    	x = a;
    	y = b;
        }
    }
    
    
    
    public class CreateObjectDemo {
    
        public static void main(String[] args) {
    		
            // Declare and create a point object and two rectangle objects.
            Point originOne = new Point(23, 94);
           
            System.out.println("X Position of originOne: " + originOne.x);
            System.out.println("Y Position of originOne: " + originOne.y);
        }
    }
    
    
    
  • This program creates, manipulates, and displays information about various objects. Here's the output:


  •  
    X Position of originOne: 23
    Y Position of originOne: 94
    
    
  • The following three sections use the above example to describe the life cycle of an object within a program.


  • From them, you will learn how to write code that creates and uses objects in your own programs.


  • You will also learn how the system cleans up after an object when its life has ended.


  • A class provides the blueprint for objects; you create an object from a class.


  • The following statement taken from the CreateObjectDemo program creates an object and assigns it to a variable:


  •  
    Point originOne = new Point(23, 94);
    
    
  • The first line creates an object of the Point class.


    1. Each of these statements has three parts (discussed in detail below):


    2. Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.


    3. Instantiation: The new keyword is a Java operator that creates the object.


    4. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.


  • Declaring a Variable to Refer to an Object Previously, you learned that to declare a variable, you write:


  •  
    type name;
    
    
  • This notifies the compiler that you will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable.


  • You can also declare a reference variable on its own line. For example:


  •  
    Point originOne;
    
    
  • If you declare originOne like this, its value will be undetermined until an object is actually created and assigned to it.


  • Simply declaring a reference variable does not create an object.


  • For that, you need to use the new operator, as described in the next section.


  • You must assign an object to originOne before you use it in your code. Otherwise, you will get a compiler error.


  • A variable in this state, which currently references no object, can be illustrated as follows (the variable name, originOne, plus a reference pointing to nothing):


  • objects-null


  • The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.


  • Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.


  • The new operator requires a single, postfix argument: a call to a 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 originOne = new Point(23, 94);
    
    
  • The reference returned by the new operator does not have to be assigned to a variable. It can also be used directly in an expression. For example:


  •  
    int height = new Point().x;
    
    
  • Here's the code for the Point class:


  •  
    public class Point {
        public int x = 0;
        public int y = 0;
        //constructor
        public Point(int a, int b) {
            x = a;
            y = b;
        }
    }
    
    
  • This class contains a single constructor.


  • You can recognize a constructor because its declaration uses the same name as the class and it has no return type.


  • The constructor in the Point class takes two integer arguments, as declared by the code (int a, int b). The following statement provides 23 and 94 as values for those arguments:


  •  
    Point originOne = new Point(23, 94);
    
    
  • The result of executing this statement can be illustrated in the next figure:


  • originOne now points to a Point object.


  • Here's the code for the Point class, which contains two constructors:


  •  
    public class Point {
        public int x = 0;
        public int y = 0;
        public int z = 0;
        //constructor
        public Point(int a, int b) {
            x = a;
            y = b;
        }
        
         public Point(int a, int b, int c) {
            x = a;
            y = b;
            z = c;
        }
        
    }
    
    
    
  • Each constructor lets you provide initial values for the Point's origin, width, and height, using primitive types.


  • If a class has multiple constructors, they must have different signatures. The Java compiler differentiates the constructors based on the number and the type of the arguments.


  • When the Java compiler encounters the following code, it knows to call the constructor in the Point class that requires a Point argument followed by two integer arguments:


  •  
     
    Point first = new Point(100, 200);
    
    
    
    
  • This calls one of Point's constructors that initializes width to 100 and height to 200.


  • All classes have at least one constructor.


  • If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor.


  • This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent.


  • If the parent has no constructor (Object does have one), the compiler will reject the program.