Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "variable initialization" Watch "variable initialization" New topic

variable initialization

may leung

Joined: Nov 28, 2000
Posts: 23
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

Joined: Dec 24, 2000
Posts: 133
Yes, if you want to access those variables directly in the main method. But you can always access non-static members via an object.

Sun Certified Java Programmer
I agree. Here's the link:
subject: variable initialization
It's not a secret anymore!