• Post Reply Bookmark Topic Watch Topic
  • New Topic

main as a static method  RSS feed

 
Amarender Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jacquie Barker!

the J2SE specification declares that static methods cannot access
non-static variables. how then the "main" method being static accesses all non-static variables. is this method an exception from the rest?
plz resolve the contradiction!

cheers,
amar
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how then the "main" method being static accesses all non-static variables.
It doesn't. Can you explain what led you to believe that the main() method "accesses all non-static variables" (i.e., code example)?
 
Jacquie Barker
author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are confusing the notion of static/class variables, instance variables, and local variables.

If you were to try to do this:

you'd get a compilation error on the line marked above, because we cannot access instance variables from within static methods.

Hope this helps!

J.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!