• Post Reply Bookmark Topic Watch Topic
  • New Topic

How come?  RSS feed

 
Francisco I
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come this one compiles?

Shouldn't it complain because I am trying to access a non static variable from a static method *when I say i=change_i(i)* within main()
Thanks!
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are actually accessing the local variable "i" and not the one declared in the class scope.
Since the main method also declares a variable named 'i', this local variable hides the instance variable "i".
Cheers!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Hima Mangal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..
does that mean a static method can access non-static local variables??

------------------
Hima
 
Rajesh Radh
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

does that mean a static method can access non-static local variables??
Hima,
1) The static/non-static modifiers does not applies to local variables.
2) local variables scope is local, and does not exist after the method is executed. (With the introduction of inner classes, this is not completely true. Because final local variables used by local inner classes can exists even after method completes its execution!)
Best wishes!
Rajesh
 
Andy Ceponis
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!