Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why cant the output of the code is 2

 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. public class Test {
2. public int aMethod() {
3. static int i = 0;
4. i++;
5. return i;
6. }
7. public static void main (String args[]) {
8. Test test = new Test();
9. test.aMethod();
10. int j = test.aMethod();
11. System.out.println(j);
12. }
13. }
What is the result?

A. 0
B. 1
C. 2
D. Compilation fails.

At line no 9 ..it calls to a.Method() where i increments and its value is 1.
At line 10 its calling to aMetod() again and it increments the value to 2.

or....is the declaration on line 10 is wrong.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static is not a modifier that you can use on a local variable.
[ June 28, 2006: Message edited by: Keith Lynn ]
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static int i = 0;

"static" modifier can not use for local variable.
 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oooooops ..I forgot tht ..I am sorry to ask this....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic