Re: explicitly initialising member vars - necessary?

From: VisionSet (spam_at_ntlworld.com)
Date: 10/25/03


Date: Sat, 25 Oct 2003 14:06:38 +0100


"Alex Hunsley" <alex.hunsley@blueyonder.munge.co.uk> wrote in message
news:o5umb.295$h55.239@news-binary.blueyonder.co.uk...
> A question on "correctness" of java code here....
> I've been lead to believe that declaring class member variables without
> explicitly initialising them (even to null or 0) is a Bad Thing(tm),
> i.e. you should write:
>
> private int count = 0;
>
> and not just
>
> private int count;
>
> However, my understanding of the java spec is that it does specify that
> member variables that aren't explicitly initialised get initialised to 0
> or null. So can anyone give me a good reason to always explicitly
> initialise member vars?

explicit - to show that you really want the attribute to have that value, it
has meaning in your code, you will probably make use of that value, whatever
that value is, even the default eg zero/null.

implicit - the implicit value (eg zero/null) is not meaningful, it is likely
that you will take care of initialisation elsewhere eg in the constructor.

--
Mike W


Relevant Pages

  • Re: Not Initializing Variables : Performance impact
    ... possible it would have a noticeable impact on performance. ... Only if your object has a very large number of member variables that you initialise. ... Eventhough creating an object instance is not very expensive in .NET, it's still so much work that a few extra instructions in the constructor won't make any measurable difference. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problems adding member variables in derived CWinApp class
    ... > I have problems adding member variables in my derived CWinApp class. ... > have an integer that I initialise in the constructor of the class but ... > in documentation that states that derived classes cant have their own ...
    (microsoft.public.vc.language)
  • Re: explicitly initialising member vars - necessary?
    ... my understanding of the java spec is that it does specify that ... > member variables that aren't explicitly initialised get initialised to 0 ... > initialise member vars? ...
    (comp.lang.java.programmer)
  • Re: grid
    ... For example, I tend to use m_ prefix for member variables, while ... others just use explicit this->. ... What is the best practice? ...
    (microsoft.public.vc.mfc)