Introduction to Java Object Class

The Object Class is the super class for all classes in Java.
Some of the object class methods are

  • equals
  • toString()
  • wait()
  • notify()
  • notifyAll()
  • hashcode()
  • clone()

An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behavior.
An object reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables.

Creating variables of your class type is similar to creating variables of primitive data types, such as integer or float. Each time you create an object, a new set of instance variables comes into existence which defines the characteristics of that object. If you want to create an object of the class and have the reference variable associated with this object, you must also allocate memory for the object by using the new operator. This process is called instantiating an object or creating an object instance.
When you create a new object, you use the new operator to instantiate the object. The new operator returns the location of the object which you assign o a reference type.

Below is an example showing the creation of Cube objects by using the new operator.

public class Cube
{
     int length = 10;
     int breadth = 10;
    int height = 10;
     public int getVolume()
    {
        return (length * breadth * height);
     }
     public static void main(String[] args)
    {
        Cube cubeObj; // Creates a Cube Reference
        cubeObj = new Cube(); // Creates an Object of Cube
        System.out.println("Volume of Cube is : " + cubeObj.getVolume());
     }
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More