Instance Control Flow and Static Control Flow are both very important control mechanisms in Java. We know that first Static Control Flow takes place and only after an Object Creation, Instance Control Flow takes place.
In this tutorial, we will understand how both control mechanisms work simultaneously through an example.
// Main Class public class Demo { // First Instance Block { System.out.println("First Instance Block"); } // First Static Block static { System.out.println("First Static Block"); } // Constructor Demo() { System.out.println("Constructor called"); } // static main method public static void main(String[] args) { // Creating first object of Demo class Demo d1 = new Demo(); System.out.println("Main method is running"); // Creating second object of Demo class Demo d2 = new Demo(); } // Second Static Block static { System.out.println("Second Static Block"); } // Second Instance Block { System.out.println("Second Instance Block"); } }
Here, first, static control flow will execute both static blocks and then it will execute the main method. Note that, static control flow only executes once.
Inside the main method, when we try to create two objects of Demo class, instance control flow will be executed twice. So, we get the same output twice here for the instance control flow.
Look at the following output for better understanding.
Output:
First Static Block Second Static Block First Instance Block Second Instance Block Constructor called Main method is running First Instance Block Second Instance Block Constructor called
The below program gives a compile-time error saying Cannot make a static reference to the non-static field d.
// Main Class public class Demo { // Double variable double d = 5.6; // static main method public static void main(String[] args) { System.out.println(d); } }
The reason again is control flow mechanism. Without creating an object, from the static area, we cannot access instance members directly.
Because while executing static area, JVM may not identify instance members as instance control flow only identifies instance members and they are not identified until we create objects.
Ways of Object Creation in Java
Following is a very important question frequently asked in interviews.
In how many ways we can create/get an object in Java?
There are 5 standard ways of creating objects in Java.
- By using new operator
Thread thread = new Thread();
- Using newInstance() method
Demo d = (Demo) Class.forName(“Demo”).newInstance();
In this technique, forName() method loads class name and creates a Class class object. Now, we are calling newInstance() method on that object. This method’s return type is Object. So, we are explicitly doing typecasting.
- By using Factory methods
Runtime r = Runtime.getRuntime();
DateFormat df = DateFormat.getInstance();
These types of methods are called factory methods. Although they use new operator inside these methods for the purpose of object creation.
- Using clone() method
Demo d = new Demo();
Demo d2 = (Demo) d.clone();
This clone() method’s return type is Object so we have to perform typecasting.
- By using Deserialization
FileInputStream fis = new FileInputStream();
ObjectInputStream ois = new ObjectInputStream();
Demo d = (Demo) ois.readObject();
This is all about the Control Flow Mechanisms and Object Creation in Java. For any doubts leave in below comment.