wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static variables" Watch "static variables" New topic
Author

static variables

Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
This is from the mock exam at http://www.geocities.com/SiliconValley/Screen/5046/ftest1.html;
The question reads:
What will happen on trying to compile and run the following application?
public class Example{
public boolean flags = new boolean[4];
public static void main(String args[]){
Example e = new Example();
System.out.println("Flag 1 is " + e.flags[1]);
}
}
choices are:
1. The text "Flag 1 is true" will be written to the standard output.
2. The text "Flag 1 is false" will be written to the standard output.
3. The text "Flag 1 is null" will be written to the standard output.
4. The compiler will object to line 2.
The correct answer is given as #2.
But I think none of the answers are correct.
It seems that the compiler should object to line #5 because a static method is trying to access a non-static variable.
Does anyone know?
Thanks in advance.
Stephanie
mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50
public class Example{
public boolean flags = new boolean[4];
public static void main(String args[]){
Example e = new Example();
System.out.println("Flag 1 is " + e.flags[1]);
}
}
Hi Stephanie,
The given answer is correct.Here the variable is accessed
by an instance e.So this is permitted.And if we don't initialise anything to the array of primitives, it is given false for boolean primitive by default.
Hope it clears your confusion.
Mita
KN
Ranch Hand

Joined: Aug 13, 2000
Posts: 66
Hi,
Don't you think it should be
public boolean flags[] = new boolean[5];
So,I think it should object to line 2.
thomas
Ranch Hand

Joined: May 26, 2002
Posts: 79
KN is right. The compiler will object to line 2.
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Guys please tell me what is wrong with line #2.

regards
vivek
KN
Ranch Hand

Joined: Aug 13, 2000
Posts: 66
Vivek,
The code is
public class Example{
public boolean flags = new boolean[4];
public static void main(String args[]){
Example e = new Example();
System.out.println("Flag 1 is " + e.flags[1]);
}
}
In line 2, you are trying to construct an array with the reference of a boolean primitive. Line 2 should be modified to
public boolean flags[] = new boolean[4];
otherwise compiler will complain. Now you are constructing an array with the reference of an array flags[] instead of flags. Hope this helps.
Kiran.
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Thanks kiran,
I miss that point may be because I didn't get my coffee this morning.

vivek
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Mita,
Thanks for your reply. It was very clear and I should have thought of that myself.
Everyone else, thanks for the input. You had very good points.
Stephanie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variables
 
Similar Threads
mock exam question
buttons in card layout
Interface of SCJP
inner class mock exam question
Sylvan On line Test link!