Re: question relates to instance variable initialization




"Shawn" <shaw@xxxxxxxxxx> wrote in message
news:ekku67$rf9$1@xxxxxxxxxxxxxxxxxxxxx
Hi,

I am unclear about the following two ways to initialize instance
variables.

public class MyClass
{
private int num = 9;

...

}

public class MyClass
{
private int num;

public MyClass()
{
this.num = 9;
}
}

What is the difference between the two? What is the consequence?

Iintializations of instance variables are effectively added to every
constructor, placed directly after the call to the superclass's constructor.
Asuming that the only constructor for MyClass() is the one shown, your two
examples behave exactly alike. To be a bit more explicit,

public class MyClass
{
private int num = 9;

public class MyClass()
{
System.out.println("made a MyClass");
}
}

is exactly the same as

public class MyClass
{
private int num;

public class MyClass()
{
super();
num = 9;
System.out.println("made a MyClass");
}
}


.



Relevant Pages

  • Re: question relates to instance variable initialization
    ... I am unclear about the following two ways to initialize instance ... public class MyClass ...
    (comp.lang.java.programmer)
  • Re: XML Serializer
    ... CDATA to escape special characters, ... semantically, the meaning of XML remains exactly the same, and, unless ... public class MyClass ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Initializing variable style question
    ... public class MyClass ...     public MyClass ... To initialize to a non-zero-like value, ...
    (comp.lang.java.programmer)
  • RE: How to reference function in shared function?
    ... Public Class Class1 ... > Public Class myClass ... > shared member initializer without an explicit instance of the class. ... > I also tried this below the class declaration: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Class/struct and Marshal.SizeOf
    ... public struct MyStruct ... public class MyClass ... public class MyStructTestClass ... SizeConst = 256)] ...
    (microsoft.public.dotnet.languages.csharp)