When ever we are executing a Java class, first static control flow executed. In the static control flow if we are creating an object, the following sequence of events will be executed as a part of instance control flow.
- Identification of instance member from top to bottom
- execution of instances variables assignment and instance blocks from top to bottom.
- Execution of constructor.
eg 1-:
class Test{
int i=10;
{
m1();
System.out.print("first instance block");
}
Test(){
System.out.print("constructor");
}
public static void main(String[] args){
System.out.print("Main");
}
public void m1(){
System.out.print(j);
}
{
System.out.print("Second instance block");
}
int j=20;
}
Explanation of above code-:
This Java class, named `Test`, has a somewhat unconventional structure, with instance blocks and field declarations mixed in an unusual way. Let's break down the code step by step:
1. `int i = 10;`: This is an instance variable declaration. The variable `i` is declared and initialized with the value `10`. It's an instance variable because it's not marked as `static`, which means each instance of the `Test` class will have its own copy of this variable.
2. Instance Block (1st): The first instance block is enclosed within curly braces `{}` and is not associated with any specific constructor. It is executed when an instance of the `Test` class is created. In this instance block:
- `m1();` is called. This method `m1` is defined below in the class.
- `System.out.print("first instance block");` prints "first instance block" to the console.
3. `public static void main(String[] args)`: This is the main method of the class. It serves as the entry point for the Java application. When you run the class, this method is called first.
4. `System.out.print("Main");`: Inside the `main` method, "Main" is printed to the console.
5. `public void m1()`: This is an instance method named `m1`. It is defined to take no arguments and returns no value. When called, it prints "Second instance block" to the console.
6. Instance Block (2nd): Another instance block is defined after the `m1` method. Like the first instance block, this block is executed when an instance of the `Test` class is created. In this block:
- `System.out.print("Second instance block");` prints "Second instance block" to the console.
7. `int j = 20;`: This is another instance variable declaration. The variable `j` is declared and initialized with the value `20`. It's also an instance variable like `i`.
Now, let's understand how the execution flows:
- When you create an instance of the `Test` class, the following happens:
- The first instance block is executed, which calls `m1()` and prints "first instance block".
- The second instance block is executed, which prints "Second instance block".
- The constructor call,it print "constructor"
- The instance variables `i` and `j` are initialized (i.e., memory is allocated for them).
- If you run the `main` method of the class, it prints "Main" to the console.
Example of how you might use this class:
public class Main {
public static void main(String[] args) {
Test test = new Test(); // Creating an instance of Test
}
}
In this example, when you create an instance of `Test`, the instance blocks are executed as described, and "Main" is not printed because the `main` method is not explicitly called.
eg-:
class Test{
int i=10;
{
m1();
System.out.print("first instance block");
}
Test(){
System.out.print("constructor");
}
public static void main(String[] args){
Test test=new Test();
System.out.print("Main");
}
public void m1(){
System.out.print(j);
}
{
System.out.print("Second instance block");
}
int j=20;
}
output-:
0
first instance block
Second instance block
constructor
Main
Note-:
- Static control flow is one activity which will be performed at the time of class loading, but instance control flow is not one time activity, and it will be performed every object creation.
- Object creation is the most costly operation, if there is no specific requirement then it is not recommanded to create objects.
Instance control flow is parent to child relationship.
Follow steps-:
- Identification of instance member from parent to child
- Execution of instance variable assignment and instance blocks only in parent class.
- Execution of parent constricter.
- Execution of instance variable assignment and instance blocks in child class.
- Execution of child constructor.
class Parent{
int i=10;
{
m1();
System.out.print("parent instance block");
}
Parent(){
System.out.print("parent constructor");
}
public static void main(String[] args){
Parent parent=new Parent();
System.out.print("parent main");
}
public void m1(){
System.out.print(j);
}
int j=20;
}
class Child extends Parent{
int x=100;
{
m2();
System.out.print("child first instance block");
}
Child(){
System.out.print("Child constructor");
}
public static void main(String[] args){
Child child=new Child();
System.out.print("child main");
}
public void m2(){
System.out.print(y);
}
{
System.out.print("child second block");
}
int y=200;
}
output-: javac Child.java
0
parent instance block
parent constructor
0
child first instance block
child second block
child constructor
child main
This Java code defines two classes, `Parent` and `Child`, and provides a `main` method in each class for testing purposes. Let's break down the code step by step:
1. `class Parent`: This is the definition of the `Parent` class.
- `int i = 10;`: This is an instance variable `i` with an initial value of 10.
- An instance initializer block: `{ ... }` This block gets executed when an instance of the `Parent` class is created. Inside this block:
- `m1();`: It calls the `m1()` method.
- `System.out.print("parent instance block");`: It prints a message to the console.
- `Parent()`: This is the constructor for the `Parent` class. It prints "parent constructor" when a `Parent` object is created.
- `public void m1()`: This is a method `m1()` that prints the value of the `j` variable.
- `int j = 20;`: This is an instance variable `j` with an initial value of 20.
2. `class Child extends Parent`: This is the definition of the `Child` class, which extends the `Parent` class.
- `int x = 100;`: This is an instance variable `x` with an initial value of 100.
- An instance initializer block: `{ ... }` This block gets executed when an instance of the `Child` class is created. Inside this block:
- `m2();`: It calls the `m2()` method.
- `System.out.print("child first instance block");`: It prints a message to the console.
- `Child()`: This is the constructor for the `Child` class. It prints "Child constructor" when a `Child` object is created.
- `public void m2()`: This is a method `m2()` that prints the value of the `y` variable.
- An instance initializer block: `{ ... }` This is another instance initializer block in the `Child` class. It prints "child second block" when a `Child` object is created.
- `int y = 200;`: This is an instance variable `y` with an initial value of 200.
3. In each class, there is a `main` method for testing:
- In the `Parent` class `main` method, it creates an instance of the `Parent` class and prints "parent main".
- In the `Child` class `main` method, it creates an instance of the `Child` class and prints "child main".
Here's the order of execution when you run the `main` methods:
- When you run `Parent`'s `main` method:
1. An instance of the `Parent` class is created.
2. The instance initializer block in `Parent` is executed, which calls `m1()` and prints "parent instance block".
3. The `Parent` constructor is called, which prints "parent constructor".
4. Finally, "parent main" is printed.
- When you run `Child`'s `main` method:
1. An instance of the `Child` class is created.
2. The instance initializer blocks in `Child` are executed in order: first one calls `m2()` and prints "child first instance block," and the second one prints "child second block."
3. The `Child` constructor is called, which prints "Child constructor".
4. Finally, "child main" is printed.
Keep in mind that the instance initializer blocks are executed before the constructor, and the order of initialization and execution is important in understanding the program's output.