This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: variable initialization
 
It's not a secret anymore!