• Post Reply Bookmark Topic Watch Topic
  • New Topic

why can this only be done when static  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the method chooseCars when I take out the static keyword I get an error but when I add the static keyword everything works fine and well,how come this is the case?

Thanks


Main

lambo.java


ferarri.java


car.java
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Non-static methods can only be called on object instances. You're calling chooseCars() from the main method, which is static, so there's no object for you to call the method on.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally think that error message is misleading. Even the term non‑static which it uses can mislead users into thinking there is something normal about being static. There isn't. If it said something like
Instance method foo() can only be called from an instance context,
you would know the correct solution is to move wherever you are calling foo() out of a static context into an instance context.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah Stephan that makes sense thanks

and yeah I agree Campbell I mean in c++ you don't have to have an instance of an object to call that method but it seems like in Java the only way to call a method inside the same class if if you make it static I wonder why?
 
salvin francis
Bartender
Posts: 1662
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, there is a difference between functions and methods. C++ has functions, Java does not.
Secondly, in your program, You are calling method "chooseCars" directly through a static context (Main.main method)
If you dont want it to be static, you could consider:

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:yeah Stephan that makes sense thanks

and yeah I agree Campbell I mean in c++ you don't have to have an instance of an object to call that method but it seems like in Java the only way to call a method inside the same class if if you make it static I wonder why?


The reason is your context. Again, main is a static method, so Java looks at the "naked" method call and assumes it is also a static method. Since it turns out that the method isn't static, Java complains. You can call instance methods from a static context if you have an object reference to dereference.

Think about it like this: a static method applies to no object in particular while an instance method does. If you have room full of people and you say "Raise your hand" to nobody in particular (let's say you were looking at the wall when you said that) everybody there would probably think you're crazy or something, right? That's like you trying to call an instance method in a static context. Now, if you looked at your friend, Brian, and said "Brian, raise your hand," then your friend will know you're talking to him specifically and asking him to raise his hand. That's like you dereferencing an object and calling one of its instance methods. Now, if instead you got up on a chair and yelled out, "Let's get this party started and raise the roof on this place! Woot! Woot!" everyone there will know that you meant to address nobody in particular so it applies to everyone (of course there's the "Let's" part, too). That's like calling a static method.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very good analogy Junilu :p that makes perfect sense

thanks
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'm still feeling a bit iffy about the last part of that analogy though. One caveat about that last part: don't take it to mean that I'm saying if you call a static method, it will apply to all instances of that class and that they're all going to start doing something. The point of the analogy was that static methods don't pertain to any specific instance of the class, whereas instance methods do, so when you call an instance method, you need to do it through a reference to a specific object. 

Technically, all method calls, whether static or instance, are made via a reference to something. When you have a "naked" method call, that is, one that is made without an explicit reference, there's still an implicit reference that Java uses.  In those cases, Java uses the context to determine what the implicit reference is.  In a static context, the implicit reference is the containing class. In an instance method, the implicit reference is "this" or the current instance.


Note how it's legal to call a static method from a non-static context (but not vice versa). Java's method call resolution rules allow this kind of call but it's discouraged because while Java can figure it out, it can be confusing to human readers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!