aspose file tools*
The moose likes Beginning Java and the fly likes Static methods confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static methods confusion" Watch "Static methods confusion" New topic
Author

Static methods confusion

Abhilash Chander
Ranch Hand

Joined: Oct 18, 2009
Posts: 39
Hi All,

I have a question about static methods. If I call a non-static method from a static method in a class, I get an error "non-static method XXX() cannot be refrenced from a static context". But if I call a non-static method which is present in some other class, it works. How is that possible ? Because its still vague which instance of the class is being used to call the method ? Hope you will thow some light on that. Thanks to all.

Abhilash
Aman S Singh
Greenhorn

Joined: May 24, 2008
Posts: 5
This is rule of thumb:

1) You can call static method from non-static context (but the reverse is not true, see 2nd point)
2) You cannot call non-static method from static context

Seems confusing, try to run this example-

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Abhilash Chander wrote:Hi All,

I have a question about static methods. If I call a non-static method from a static method in a class, I get an error "non-static method XXX() cannot be refrenced from a static context". But if I call a non-static method which is present in some other class, it works. How is that possible ? Because its still vague which instance of the class is being used to call the method ? Hope you will thow some light on that. Thanks to all.

Abhilash


Static methods are loaded at class loading time. And it's belonging to class, not to a single instance, so you can call that method without any instances. But to call a non static method, you need an object. Because it's belonging to instances of the class. If you call the non-static/instance method from static/class method, compiler can't determine, on which object you've called the instance method. So, it won't allow it!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abhilash Chander
Ranch Hand

Joined: Oct 18, 2009
Posts: 39


Static methods are loaded at class loading time. And it's belonging to class, not to a single instance, so you can call that method without any instances. But to call a non static method, you need an object. Because it's belonging to instances of the class. If you call the non-static/instance method from static/class method, compiler can't determine, on which object you've called the instance method. So, it won't allow it!



I agree what you are saying. But I have two cases, First Case :


Main Class




SECOND CASE

Main Class




TempMain




Here FIRST CASE gives me an error for calling a non-static method from a static context whereas SECOND CASE works. Now the only diffrence between the two is, in FIRST CASE the non-static method is in the same class and in SECOND CASE its in another class. But the calling is same for both cases i.e from a static context to a non-static method. Then howcome one works other doesnt. Still not clear why its happening
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

In the first code, you've called an instance method on a static method without an instance. And, in the second case, you've an instance named tm, so can call the instance method of that class on this instance!
Abhilash Chander
Ranch Hand

Joined: Oct 18, 2009
Posts: 39
Abimaran Kugathasan wrote:In the first code, you've called an instance method on a static method without an instance. And, in the second case, you've an instance named tm, so can call the instance method of that class on this instance!


Thanks . Got it !!!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Abhilash Chander wrote:
Thanks . Got it !!!

Welcome!
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

think of it like this,

You can call a static from a non static

but the other way round, from a static to call a non-static won't work. Static itself doesn't know anything about instance members, so which one would it try to call? it can't


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods confusion