Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are java object fields initialized with?

Is it null for Object type?

class C {
    int i;
    String s;
    public C() {}
}

Will s be always null?

What about simple types as int? What will that be? Zero or an arbitrary value?

What about local variables in methods?

public void meth() {
    int i;
}

What is the unitialized value of i?


Relying on such default values, however, is generally considered bad programming style.

Ok, what do you suggest we do?

class A {
    String s = "";
    int i = 0;
}

OR:

class A {
    String s;
    int i;
    public A() {
        // default constructor
        s = "";
        i = 0;
    }
}

Which is better and why?

like image 248
EugeneP Avatar asked Dec 16 '09 08:12

EugeneP


People also ask

How are fields initialized in Java?

Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods. The Java compiler copies initializer blocks into every constructor.

How is an object initialized in Java?

Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

What is used to initialize an object?

Objects can be initialized using new Object() , Object. create() , or using the literal notation (initializer notation).

What is initialized in Java?

In Java, an initializer is a block of code that has no associated name or data type and is placed outside of any method, constructor, or another block of code. Java offers two types of initializers, static and instance initializers.


2 Answers

From suns java tutorial

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

The following chart summarizes the default values for the above data types.

Data Type   Default Value (for fields)
byte                    0 
short                   0   
int                     0 
long                    0L 
float                   0.0f 
double                  0.0d 
char                    '\u0000' 
boolean                 false
String (or any object)  null 

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

like image 77
michiel Avatar answered Oct 13 '22 07:10

michiel


For member variables: The default value for String is null. The default value for primitives is 0 (or 0.0 for floating point values).

For local variables: You must explicitly initialise a local variable before using it.

As to the second part of your question: You can always say String s = ""; in the member variable definition, or s = ""; in the constructor. Then you know it will have a non-null value. (Also, in your setter you'd need to ensure that someone doesn't try and set it back to null.)

like image 45
Ash Avatar answered Oct 13 '22 06:10

Ash