Java Create Object

Any C++ programmer would say, an instance of a class is known as object, when asked what an object is. It is a very confusing statement for a novice. I explain in my own way. Now imagine there is trunk of wood. The wood as it is waste until and unless converted into articles like door, table and duster etc. No door, no table and no duster, the wood is mere waste. We say, they are instances of wood. Other way, they are objects of wood. Let us dig more through Java Create Object.

Think, in place of wood, the class Test. The Test class contains the constructs like constructor, method and variables. To make use of the constructs, we require an instance or object. No instance or object, the class is mere waste. The t1 is object with which we can call all the constructs. For this reason, object creation is known as instantiation or we say object is instantiated.

Example on Java Create Object.
public class Test
{
  int mangoes = 10;                                 // it is variable
  public Test()                                     //  it is constructor
  {
    System.out.println("Constructor is called");
  }
  public void show()                                //  it is method
  {
    System.out.println("Method is called");
  }
  public static void main(String args[])
  {
    Test t1 = new Test();                           // implicitly, constructor is called
    
    System.out.println("No. of mangoes: " + t1.mangoes);  // variable is called
    t1.show();                                      // method is called
  }
}


Java Create Object

Output Screenshot on Java Create Object

Here, t1 is called as object of Test class. We say, t1 object is instantiated. It can be said, t1 is a pointer for the whole class Test with which all the constructs of the class like mangoes, show() etc. are called.

Full notes with examples on object instantiation (creation) and anonymous objects is available at Java Reference Variables – Objects – Anonymous objects.

Read this when you are very comfortable with Core Java: Do you know that you can create object without new keyword also. You know, 5 styles are there.

2 thoughts on “Java Create Object”

  1. sir , i have one doubt in above program for void show() if we change return type i.e int show() .it is showing an error
    class Baba
    {
    int apple=10; // variable
    public Baba() // constructor
    {
    System.out.println(“thasis is constructor “);
    }
    public int Baba() // method
    {
    System.out.println(“this is method :”);
    }
    public static void main(String args[])
    {
    Baba b1= new Baba();
    b1.Baba();
    System.out.println(“apple is :”+b1.apple);

    }
    }

Leave a Comment

Your email address will not be published.