Re: question relates to instance variable initialization
- From: "Mike Schilling" <mscottschilling@xxxxxxxxxxx>
- Date: Thu, 30 Nov 2006 02:16:05 GMT
"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");
}
}
.
- References:
- Prev by Date: Re: Bogus NullPointerExceptions
- Next by Date: Re: Giving an application a window icon in a sensible way
- Previous by thread: Re: question relates to instance variable initialization
- Next by thread: Re: question relates to instance variable initialization
- Index(es):
Relevant Pages
|