Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

why cant the output of the code is 2

 
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.
 
Ranch Hand
Posts: 2410
  • 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 ]
 
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....
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic