This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variable initialization

 
may leung
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Base{
private int i;

static public void main(String[] a) {
System.out.println("Value is: " + i);
}
}
Select most appropriate answer(s).
a) Value is: 0
b) Compile time error. Can't access the private variable i defined in class Base.
c) Compile time error. Can't make a static reference to nonstatic variable i in class Base.
d) Runtime error. Variable i is uninitialized
e) Compile time error. Variable i is uninitialized
f) None
the ans is C. is that mean all the variable used in the main method should be static?? Can anybody clarify for me??
Thank you~~
 
Tom Tang
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you want to access those variables directly in the main method. But you can always access non-static members via an object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic