This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why cant the output of the code is 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why cant the output of the code is 2" Watch "why cant the output of the code is 2" New topic
Author

why cant the output of the code is 2

Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 02, 2006
Posts: 2023
static int i = 0;

"static" modifier can not use for local variable.
Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
oooooops ..I forgot tht ..I am sorry to ask this....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why cant the output of the code is 2
 
Similar Threads
post(not)incremented?
why compile error??????/
Some problem
About the static
method