This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods confusion

 
Abhilash Chander
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Abhilash Chander
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhilash Chander wrote:
Thanks . Got it !!!

Welcome!
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic