Forums Register Login

Doubt with declaring local variables as satic

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
First appropriate declaration; then comes initialization.
+Pie Number of slices to send: Send
There is only one modifier can be used by local variables that is final modifier.
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 838 times.
Similar Threads
Non static method
java
Reason for Compile-Time error
static variable within a non-static method
local variable!
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:21:25.