Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "method" Watch "method" New topic
Author

method

xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
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?
Shawnne James
Greenhorn

Joined: Oct 27, 2005
Posts: 13
The result will be a compile time error because of the line 3 "static int i = 0;". You cannot use static for a local variable. The error message will be "illegal start of expression".

Please correct me if I'm wrong.
Kian Ng
Greenhorn

Joined: Dec 18, 2005
Posts: 17
Compilation error? cos i remember static modifier cannot be used on local variables in methods... onli final can be applied. hope it answer your question
xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
thank you
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: method
 
Similar Threads
About the static
Doubt in Static Method Access.
why cant the output of the code is 2
why compile error??????/
Static variable