This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Confusion in static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion in static methods" Watch "Confusion in static methods" New topic
Author

Confusion in static methods

sanchit latawa
Greenhorn

Joined: Jul 26, 2010
Posts: 6
Hi,I'm confused regarding the use of static method.As you can only call static methods and variables inside a static method .So this means

This is illegal

But than why is this legal ???


Also you can not use any non static variable inside a static method but when i do something like

So this works but if i was declared outside main and I tried to use it this would not.Also if i declare a variable or method inside static method .Does it automatically become static ??
THanks
Prashant Hurria
Ranch Hand

Joined: Mar 23, 2009
Posts: 40


This is illegal becuse you are reffering a non static method from a static code. Non static members have to be assosiated with a object.





So this works but if i was declared outside main and I tried to use it this would not.Also if i declare a variable or method inside static method .Does it automatically become static ??


Dont be confused between static and local variables. Read a basic book on Java like Head First
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please indent your code; unformatted code is unnecessarily difficult to read.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion in static methods