Menu Close

Create Object in Java

Creating object:

  • Execution starts with static context
  • To access non static context, object is required.
  • We create object in static context.
  • We can create object using any one of 4 static members
    • static main()
    • static block
    • static variable
    • static user method

Creating object in main():

  • ‘new’ operator allocates object memory.
  • object memory contains instance variables.
  • we must invoke constructor in object creation process.
class Test 
{
	Test()
	{
		System.out.println("Constructor");
	}
	public static void main(String[] args) 
	{
		new Test();
	}
}

Constructor will be called every time when we create object.

class Test 
{
	Test()
	{
		System.out.println("Constructor");
	}
	public static void main(String[] args) 
	{
		new Test();
		new Test();
		new Test();
	}
}
  • Instance block always execute before constructor.
  • JVM access instance block as soon as object has created.
class Test 
{
	Test()
	{
		System.out.println("Constructor");
	}
	{
		System.out.println("Instance block");
	}
	public static void main(String[] args) 
	{
		new Test();
	}
}
  • static block executes when program execution starts.
  • instance block and constructor executes every time when object has created.
class Test 
{
	static
	{
		System.out.println("Static block");
	}
	Test()
	{
		System.out.println("Constructor");
	}
	{
		System.out.println("Instance block");
	}
	public static void main(String[] args) 
	{
		new Test();
		new Test();
		new Test();
	}
}

Default constructor:

  • Compiler will add a zero arguments constructor to the source is called default constructor.
  • It will be added when we don’t define any constructor.
  • Default constructor has empty definition.
  • Defining constructor is optional.
  • Default constructor executes when we create object
class Test 
{
	/* Test()
	{
		default constructor has no logic...
	} */
	public static void main(String[] args) 
	{
		new Test();  // no error : invokes default constructor
	}
}

Object reference variable:

  • Once object has been created, constructor returns address.
  • We collect the address into a variable called ‘Object reference variable’
  • Object reference variable is ‘Class type’
Class_name  var = address ;
  • When we display object address, it will display Class name and Memory location in Hexadecimal format.
class Test 
{
	Test()
	{
		System.out.println("Object constructed...");
	}
	public static void main(String[] args) 
	{
		Test obj = new Test();
		System.out.println("Address : " + obj);
	}
}

Non static method:

  • We define 2 types of methods static and non static.
  • Static methods need to define using static modifier
  • Static methods can be access using class name.
  • Non static methods must be accessed using object address.
class Test 
{
	public static void main(String[] args) 
	{
		Test address = new Test();
		Test.m1();
		
		// Test.m2(); -> Error :
		address.m2();
	}
	static void m1()
	{
		System.out.println("Static method");
	}
	void m2()
	{
		System.out.println("Non static method");
	}
}