Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of initialization of data fields in java

Tags:

java

class

In the code below, what is the order of initialization of data fields? What is the general rule followed by java for data member and member functions?

public class TestClass 
{
  int j=10;
  static int h=5;

  public static void main(String[] args)
  {
     TestClass obj= new TestClass();       
  }
}
like image 723
user3525139 Avatar asked Feb 06 '26 14:02

user3525139


2 Answers

In general:

1) static field members (static initializers in general)

2) non-static field members

3) constructor

However you can test it with a snippet of code like this:

public class TestClass {
    int i = 10;
    static int j = 20;
    public TestClass() {
        // TODO Auto-generated constructor stub
        System.out.println(i);
        i = 20;
        System.out.println(i);
    }
    public static void main(String[] args) {
        new TestClass();
    }
}

Quoting from the great "Thinking In Java":

Within a class, the order of initialization is determined by the order that the variables are defined within the class. The variable definitions may be scattered throughout and in between method definitions, but the variables are initialized before any methods can be called—even the constructor. ................................... There’s only a single piece of storage for a static, regardless of how many objects are created. You can’t apply the statickeyword to local variables, so it only applies to fields. If a field is a staticprimitive and you don’t initialize it, it gets the standard initial value for its type. If it’s a reference to an object, the default initialization value is null.


To summarize the process of creatingan object, consider a class called Dog:

  1. Even though it doesn't explicitly use the static keyword, the constructor is actually a static method. So the first time an object of type Dog is created, or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does by searching through the class path.

  2. As Dog.class is loaded (creating a Class object, which you’ll learn about later), all of its static initializers are run. Thus, static initialization takes place only once, as the Class object is loaded for the first time.

  3. When you create a new Dog( ), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.

  4. This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for numbers and the equivalent for boolean and char) and the references to null.

  5. Any initializations that occur at the point of field definition are executed.

  6. Constructors are executed.This might actually involve a fair amount of activity, especially when inheritance is involved.

like image 127
mohsen kamrani Avatar answered Feb 09 '26 07:02

mohsen kamrani


Here is the order.

  1. Static fields initialized and static block executed at class loading time, they will be initialized in the same order they declared.
  2. Set fields to default initial values (0, false, null)
  3. Call the constructor for the object (but don't execute the body of the constructor yet)
  4. Invoke the constructor of the superclass
  5. Initialize fields using initializers and initialization blocks
  6. Execute the body of the constructor
like image 23
Abimaran Kugathasan Avatar answered Feb 09 '26 07:02

Abimaran Kugathasan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!