1

Which is better or which is right way to do this, can you also give explanation (I didn't know what search term to use on Google).

First way:

Public Class A()
{
    Paint _paint _test;

    public void running()
    {
        _test = new Paint();
       //use paint
    }
}

OR

Public Class B()
{
    Paint _paint _test = new Paint();

    public void running()
    {
        //use paint
    }
}

Thanks

Oliver Dixon
  • 7,012
  • 5
  • 61
  • 95
  • 1
    Apart from the fact that none of the 2 would compile, both are valid and the answer depends on the context and what you are trying to achieve. – assylias Apr 26 '12 at 20:25
  • [Should I initialize variable within constructor or outside constructor](http://stackoverflow.com/questions/3918578/should-i-initialize-variable-within-constructor-or-outside-constructor). – Lion Apr 26 '12 at 20:29

3 Answers3

4

The first way is better when you might (maybe in the future) possibly want to pass parameters to the member's constructor, e.g:

class Foo{
    private Bar bar;

    public Foo(){
        bar = new Bar();
    }

    public Foo( String s ){
        bar = new Bar( s );
    }
}

The second way is better when you know you will never want to pass parameters to the member's constructor and you have multiple constructors that all need to initialize this member, e.g:

class Foo {
    private Bar bar = new Bar();

    public Foo( String s ){ ... }
    public Foo( int i ){ ... }
    public Foo( double d ){ ... }
    public Foo( String s, int i ){ ... }
}
trutheality
  • 23,114
  • 6
  • 54
  • 68
0

The answer is 'it depends'. In the case you have illustrated it is probably more elegant to use the second option. However if a value needs to know the value of a constructor parameter or is in any way altered by the construction of the class it should probably be initialised in the constructor. If a member can be initialised at its declaration it should be. This will prevent the need to initialise them to 0 or Null when the class is allocated.

Will
  • 4,585
  • 1
  • 26
  • 48
-1

It is best to initialize just before it is used. Only initialize variables that are always required/used in the constructor.

Vivek Viswanathan
  • 1,968
  • 18
  • 26
  • So? variable's I initialize are always used though so class A is better? – Oliver Dixon Apr 26 '12 at 20:26
  • lets take an example of service class & dao class. The dao class is required in service class & does not depend on the constructor arguments so its better to initialize it like classB. If a config parameter needs to be initialized based on constructor arguments, then follow approach of classB. If there is an ArrayList being used in one of the processng, it is better to initialize just before you use it inside the method. – Vivek Viswanathan Apr 26 '12 at 20:31
  • -1. Instance members are always used, otherwise they should be removed. The entire purpose of initialization and construction is to get the object into a usable state. Your remark applies to local variables in methods but it would be better stated as 'it is best to *declare* just before it is used'. – user207421 Apr 27 '12 at 00:12