• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variable initialization

 
vinita Kh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic