Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variables

 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KN is right. The compiler will object to line 2.
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys please tell me what is wrong with line #2.

regards
vivek
 
KN
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks kiran,
I miss that point may be because I didn't get my coffee this morning.

vivek
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic