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.
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.
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.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database