Re: Initializing variable style question



On Nov 13, 12:13 pm, Nicros <bcc...@xxxxxxxxx> wrote:
On Nov 13, 9:10 am, Nicros <bcc...@xxxxxxxxx> wrote:

Between:
public class MyClass
{

I have to say, using groups through google SUX.

The question is, between
public class MyClass
{
    int x = 0;
    public MyClass ()
    {
    }

}

and

public class MyClass
{
    int x;
    public MyClass()
    {
        x = 0;
    }

}

Is there a valid reason to initialize stuff in the constructor only?
Is it a style only thing?

Don't use either - initialize a variable to its "zero-like" value
(zero or null) is redundant either way. It forces the variable to
that value twice, once when the member is constructed and again when
the initializer or constructor is executed.

public class MyExample
{
int x;
}

will do the job.

To initialize to a non-zero-like value, either way will do. I prefer
initializers, because it puts the initialization together with the
declaration, but others prefer to use the constructor. Sometimes it
makes a difference - if you need to initialize a variable to the same
value for many constructors, put it in the declaration. If you need
to initialize it to a different value depending on the constructor, it
must go in the constructor. 'final' variables can and must be
initialized exactly once, so that usually determines where you put
it. 'final' instance variables must be explicitly initialized even to
a zero-like value. Constants should be initialized in the
declaration.

public class MyExample
{
private static final int LENGTH = 16;
private final String names [] = new String [LENGTH];

private boolean ready;
private String foo;

public MyExample()
{
this.foo = "";
}

public MyExample( String firstFoo )
{
this.foo = firstFoo;
}
// etc.
}

--
Lew
.



Relevant Pages

  • Re: Initializing Member Variables of Class ...
    ... I was wondering what the proper way is to initialize member variables ... Public Class ...   Public Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Still problems with JTable
    ... But EVERYTHNG is processed by myRenderer. ... public class MyJTable extends JTable //implements TableModelListener ...     protected TableModel createDefaultDataModel ... public class MyJTableDemo ...
    (comp.lang.java.help)
  • Re: Please explain this polymorphism twist to me.
    ... public class OverloadTest { ...     public class Visitor { ... confirms that the actual type of "this" is Sub! ...
    (comp.lang.java.programmer)
  • Re: C# syntax
    ... public class Customer ...     public Customer(string strID, string strName, string strAddress, ... constructor, since you've explicitly defined a constructor with ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Business Object Collection And DataGridView
    ...   object m_oValue; ... public class MyObject ... I set the datasource of my DataGridView with my homemade collection. ... sub-property Value of the more complex object Attribute in my class MyObject ...
    (microsoft.public.dotnet.languages.csharp)