Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with declaring local variables as satic

 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which option most fully describes will happen when you attempt to compile and run the following code..

The output is given as option 4:..why cant be the output be option 2..since i has not initialized and used here..

public class MyAr{
public static void main(String argv[]) {
MyAr m = new MyAr();
m.amethod();
}
public void amethod(){
static int i;
System.out.println(i);
}
}

1) Compilation and output of the value 0
2) Compile time error because i has not been initialized
3) Compilation and output of null
4) Compile time error
 
Peter MacMillan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct in that i has not been initialized. However, the more important issue is the use of the static keyword.

Inside of an instance method, a static modifier makes no sense at all. Static variables must be fields of the class.
[ June 27, 2006: Message edited by: Peter MacMillan ]
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First appropriate declaration; then comes initialization.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one modifier can be used by local variables that is final modifier.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic