aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes variable initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "variable initialization" Watch "variable initialization" New topic
Author

variable initialization

vinita Kh
Ranch Hand

Joined: Feb 19, 2002
Posts: 49
Can nebody plz explain y this code doesn't give compiler error, saying int temp in constructor may not have been initialized.
is it because its assigned a static value into it?
public class Test3 {
static int total = 10;
public static void main (String args []) {
new Test3();
}
public Test3 () {
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}
}
}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by vinita Kh:
Can nebody plz explain y this code doesn't give compiler error, saying int temp in constructor may not have been initialized.
is it because its assigned a static value into it?

Yes.
Actually, it's not really that important that you're assigning a static value; a regular integer value would have worked as well, for example:
int temp = 5;
The point is, you have initialized temp by assigning it a value before it is used, so there are no compiler errors here.


Rob
SCJP 1.4
 
jQuery in Action, 2nd edition
 
subject: variable initialization