2

Possible Duplicate:
Should I initialize variable within constructor or outside constructor

For example I have a field int x. Does Java prefer the field to be initialized when it's declared

int x = 0;

or rather in the constructor?

public foo() {
x = 0;
}

Which is more preferred from a designing perspective?

Community
  • 1
  • 1
Luke Taylor
  • 9,481
  • 13
  • 41
  • 73

6 Answers6

1

Both are good, as long as you know what will happen.

The order in which they will be initialized is this:

  1. Class members (like: public int x = 0;)
  2. Constructors (like: this.x = 0;)

However, initializing integers to zero is a no-op. Java does this automatically.

A little demo to demonstrate an error of ignoring the order of initialization:

class Foo
{
    public String str;
    public String strTwo = "Here is str: " + str;

    public Foo()
    {
        str = "Java";
        System.out.println(strTwo);
    }
}

This will print Here is str: null.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

public foo() { x = 0; }

this will be good. if u use this int x =0 it will be for all objects. but the constructor notation is for objects which are created by that particular constructors.

Nagaraju Badaeni
  • 890
  • 8
  • 14
0

Instance variables of primitive types are initialized implicitly whereas reference variable are assigned with null values.

So if you want to assign like ::

int x=0;

it will be x=0 for all objects.

But in case of constructors you can initialize them with different value in different constructor, though its not a good practice to initialize with different values in different constructor.

Prateek
  • 12,014
  • 12
  • 60
  • 81
0

Does Java prefer the field to be initialized when it's declared or rather in the constructor?

If you are initializing with default value, it'll be redundant to initialize either in constructor or at declaration time.

However, if you want to initialize with a value different from default value, constructor should be used over delaration.

Which is more preferred from a designing perspective?

From design point of view, No difference at all. It is implementaiton detail.

Azodious
  • 13,752
  • 1
  • 36
  • 71
0

My preference is to initialize default values outside constructor like

int x=0;

So that if you have multiple constructors and lot of instance variables, you won't forgot to initialize. In-case if I require specific value to variable, then I will re-initialize in constructor.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
0

From what I recall, the bytecode produced by the compiler in both instances is pretty much identical, and it is really just a matter of preference and coding style. Personally, I tend to prefer to initialize variables in the constructor - In some cases you will have to initialize variables in a constructor anyway (Such as when passing in arguments to a constructor), and it means you need only look in one place to understand what is going on, rather than 2.

(On a side note - in your example there is no need to do "int x = 0" since defining "int x;" will automatically have it set to 0.)

tofarr
  • 7,682
  • 5
  • 22
  • 30