Default Initialization of Private Fields in Java

java initialize instance variables outside constructor
java variable initialization best practices
object initialization in java
the fields need not be initialized before use
how to initialize class variables in java
java class initialization order
3 ways to initialize object in java
variable initialization in java

Say I have a class called Person:

class Person{
    private int num = 1;
}

When calling the empty constructor for class Person, will Java first initialize num to a default value of zero, and then assign num to 1? Or will Java initialize num to 1 straight away? Doesn't C++ do the former unless you use initializer syntax?

Thank You!

It would assign the value zero first.

If you put something before the field declaration like so, you could see this:

class Person{
    { print(); }
    private int num = 1;
    { print(); }

    void print() {
      System.out.println(num);
    }
}

This would print:

0
1

This is described in JLS Sec 4.12.5:

Each class variable, instance variable, or array component is initialized with a default value when it is created 

And Sec 15.9.4 (emphasis added and some parts omitted):

The new object contains new instances of all the fields declared in the specified class type and all its superclasses. As each new field instance is created, it is initialized to its default value (§4.12.5).

...

Next, the selected constructor of the specified class type is invoked ...

In other words, object creation and constructor invocation are distinct operations, and the fields are initialized to their default values prior to the invocation of the constructor.

The actual field initialization occurs in the constructor: field initializes are inlined into every constructor that invokes super(...), immediately after the call to super(...). The following is exactly equivalent to the above:

 class Person{
   private int num;

   Person() {
     super();
     print();
     num = 1;
     print();
   }

   void print() {
     System.out.println(num);
   }
}

One subtle point to make it that although declaring the field final would make the output of this code:

1
1

This does not mean that final variables are initialized sooner. All that has changed in that case is that num has become a compile-time constant expression, and thus is inlined. As such, print() would actually be executing:

System.out.println(1);

In other words, it does not read the field at all.

What is the default initialization of an array in Java?, What is usually used to initialize fields in an object? In other words, object creation and constructor invocation are distinct operations, and the fields are initialized to their default values prior to the invocation of the constructor. The actual field initialization occurs in the constructor: field initializes are inlined into every constructor that invokes super() , immediately after the

Regarding the Java Docs:

Fields that are declared but not initialized will be set to a reasonable default by the compiler.

Considering this enunciation, I would say that Java will assign the value straight away.

Java: define terms initialization, declaration and assignment, of its type when the class is first loaded. A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code. There is an alternative to static blocks — you can write a private static method:

boolean false
byte    (byte) 0
short   (short) 0
int 0
long    0L
char    \u0000
float   0.0f
double  0.0d
object reference    null

Here are all the default values of Java. It will be init by this by default any variable with this. So with answer of Andy I can conclude that it will be always first 0 then it will go by the value you give to the variable.

A Guide to Java Initialization, Instance variables can be initialized in constructors, where error handling or There is an alternative to static blocks — you can write a private static method:. Classes and objects in Java must be initialized before they are used. You've previously learned that class fields are initialized to default values when classes are loaded and that objects are initialized via constructors, but there is more to initialization. This article introduces all of Java's features for initializing classes and objects.

Why do instance variables have default values in java?, public final class Quark { public Quark(String aName, double aMass){ fName = aName; fMass = aMass; } //PRIVATE //WITHOUT redundant initialization to default  When writing code that initializes different types of fields, of course, we have to keep an eye on the order of initialization. In Java, the order for initialization statements is as follows: static variables and static initializers in order; instance variables and instance initializers in order; constructors; 10. Object Life Cycle

Initializing Fields (The Java™ Tutorials > Learning the Java , 1. Default Initialization of Instance Variables in Java. When you declare a variable without assigning it an explicit value, the Java compiler will  Initialize class fields in constructor This works well when the initialization value is available and the initialization can be put on one line. However, this form of initialization has limitations because of its simplicity.

Java Practices->Initializing fields to 0 false null is redundant, Learn how to to initialize Java classes and objects for successful JVM execution. You've previously learned that class fields are initialized to default values when classes are class City { private String name; int population; City(String name,  As we know, in java and some other object-oriented programming languages, fields value can be set in constructor or be initialized in fields declaration statements. I want to know the essential differences between the two ways above.

Comments
  • How would you tell the difference?
  • I'm not sure. I figure it would be best to initialize fields to the value you specified and not spend time initializing them to a default value.
  • initializing all the class variables to their default value is really easy: just set a contiguous block of memory to all zeros. This strategy is fast and easy to implement - a lot easier than trying to work out what isn't initialized (say, if variables are set conditionally), and so is obviously less vulnerable to problems with using uninitialized memory, making it secure. Don't try to second guess the performance, but also don't discount benefits beyond performance.