This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

how to call non static code from static context  RSS feed

 
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from main class we cannot call non static code with in the same class, but you can call it from another class why not from same class?
 
Marshal
Posts: 6255
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You need to create an instance of the class.

And if you think for a while, it makes sense. You can't call from static method a non-static method NOT because you are trying to call it from static method, but because in order to call non-static method, you need to have an instance of the class. That might be a bit confusing as you may think: "hang on, but class doesn't fail to compile if I call non-static method from a non-static method", - yes, that is true, but in order to call such method(s), instance would need to be created first, so it sort of ensures that at the moment you'll call such method(s), instance will be created for sure.

And that is true for calling such method(s) from other class. If you have classes A and B, from class A you can call B class's instance methods only via the instance of the class B. If the method appears to be a static, that means you can call such method without having an instance of the class B, regardless if you are calling it from static or non-static method.
 
Sheriff
Posts: 12739
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sohail hussain wrote:from main class we cannot call non static code with in the same class, but you can call it from another class why not from same class?


Please provide the code that you're trying to run so we have more context.

Non-static methods, otherwise known as instance methods, must be called in reference to an object. Without seeing what you actually tried, I can guess it might be something like this:

You are probably asking about something you did similar to line 6 above which is illegal because in a non-static context, there is no object that is associated with the call to bar(). That is, you're trying to call an object's method without saying which object you're calling it on!

Given the above Listing 1 code and assuming we comment out line 6, you can always write this:

This code can be put in a static or non-static method and it can be any class, whether it's in Foo.main() or some other class. On line 4, the call is made to the bar() method of the object referenced by foo. In fewer words, we say that "we are calling foo.bar()"

You may ask why the call to bar() in Listing 1, line 13 is legal. That's because in instance methods, any method calls that do not have explicit object references attached to them are assumed to be using the current object, represented by "this". That is, that method is equivalent to:

The implicit "this." reference is only used in instance methods to try to resolve method calls by dereferencing the current object.




   
 
Marshal
Posts: 61710
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again.

I personally think that error message is confusing. It gives you the impression there is something normal about code being in a static context. Regard it as unusual, a breach of normal practice to have anything static. That means the main method is always an unusual context. I also think it confuses beginners to hear something called non‑anything, especially non‑static. If it had said,

Instance code can only be called from an instance context,

I think that would have been a much clearer error message.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!