5

I was wondering whether I should initialize class members in java with an initial value and then change that value to some other given value in the constructor, or should I avoid doing such a thing?

code example

public class Test {
    private int value = 5;

    public Test(int value) {
        this.value = value;
 }
}
3

5 Answers 5

12

If not specified,:

primitive bytes, shorts, ints, longs, floats and doubles are initialized to 0

booleans are initialized to false

Objects are initialized to null

Sign up to request clarification or add additional context in comments.

2 Comments

Not to mention primitives float and double which are initialized to 0.0
Please update it correctly ;) byte, short, int ànd long are initalized to 0. float and double are initialized to 0.0 (there is a difference between 0 and 0.0 (for floating point types at least)). char is also a primitive and its initial value is \u0000.
5

If we are talking about class fields than all unset variables of

  • primitive types are set to
    • 0 (numeric ones like int, long, double...)
    • \u0000 (char)
    • false (boolean).
  • object types like String, Integer, or AnyOtherClass are set to null

so actually it doesn't matter if you set it explicitly, because

private int x;    
private Integer y;

is equivalent of

private int x = 0;    
private Integer y = null;

Comments

1

Java give value class variables, I mean they are initialized by JVM and you can use them. But you must to search their default values to use them correctly.

On the other hand, JVM does not initialize the local variables which is created in methods. So if you create any variable on methods you have to assign them to a value before use them.

1 Comment

Welcome on SO! In your answer, you refer to another answer. Note that "above" and "below" may change at any time, thus you should comment on an answer if you wish to add something to it instead of posting a new answer with your additions. To comment on an answer you need to have gained some reputation first, though.
0

A not initialized int is always 0 on heap. It can't never be null. Mind: within a method it must be initialized, not only declared.

Comments

0

First, you cannot initialize an int value with null. The default value of an int is zero, but initializing it in both the field declaration and the constructor is pointless. Assuming the compiler does not omit the field initializer completely, it effectively compiles to this:

public class Test {
    private int value;

    public Test(int value) {
        this.value = /* field initializer value */;
        this.value = value;
    }
}

Unless you want to initialize a field with a non-default value (non-zero, non-null), there is generally no reason to add an initializer. This is especially true if the initializer is redundant, as is the case above. The default value for a field is the "zeroed out" value of the field type, e.g., 0 for numeric types, false for a boolean, and null for objects. Note that there is an exception with respect to fields: final fields must have an initializer if they are not assigned exactly once in every constructor; final fields have no implicit default value.

Now, it may be necessary to initialize method variables to guarantee they have a value by the time they are read, as variables do not have a default/implied initializer as fields do. Whether an explicit initializer is necessary depends on the control flow of your method.

3 Comments

Some teams enforce initializing objects to null to quiet the IDE complaining 'xyz variable might not have been initialized', so its a good practice to initialize them to null in those cases.
@JonathanCole I'd say that's a bad practice, unless null is specifically the expected, valid value in those cases. Otherwise, if the IDE says the variable might not be initialized, then there is likely a code path where it doesn't get initialized, and you should fix that problem.
Way old but just saw @Mike Strobel, response and I agree Mike, I was indicating its good practice when team standard requires it. Also in this case the path is almost always within method scope, and the null checks are needed (so null is the 'expected' value in your statement). I agree some teams have 'bad practices' and sometimes consistancy must win the day (from the team's perspective), otherwise we should employ defaults or value objects as Mike suggests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.