No it's not. It's calling the method for the instance of the object you put into obj (please try for better variable names). That gives it a non-static context. In your first example. you're just calling it withou any instance.
An object of class can invoke its method. Here you created an object of Cat class and void show is method of Cat class so you can call it from main. For non-static method you need to create an object of the class which contains this method. For static method you don't need an object you can directly invoke it by just using method name Ex. show(). Here you declared void show() as non-static method and trying to invoke without using object of the class where it belongs to...
Joined: Aug 31, 2012
@Bear Bibeault :
i could not understand:
It's calling the method for the instance of the object you put into obj.That gives it a non-static context.
Can you explain it a bit further?
from what you said ,i think a object of a class defined in static method main() can call any methods present in the same class irrespective whether it's static or not.
Is this right?
No Rohan you don't need an object to call static method. yes you can call a method of class using an object of that class. See that's why main is a static method so compiler doesn't require an object to invoke main method because it's static method...
Bear Bibeault wrote:No it's not. It's calling the method for the instance of the object you put into obj (please try for better variable names). That gives it a non-static context. In your first example. you're just calling it withou any instance.
You wrote "That gives it a non-static context." . The error came was that we cannot call non-static show() method from static context, therefore, when we made an instance and a reference variable-obj, to that instance, That gave it static context. Isn't it should be like this ???
Non-static methods need to be called on a specific object - an instance of the class. If you're already in a non-static method, you've already got a suitable object, which is the one represented by this. Calling show() is equivalent to calling this.show().
But in a static method, there is no this available - you aren't in an object. That's what is meant by a "static context". So you need to specify which object it's being called on. That's what obj.show() does - "call the show() method on the object referenced by obj".
Think about it. All programs start in the main method. If there was no way at all to call a non-static method you could never call anything!
A simple rule for calling methods whether static or non-static is :
You can't call something that doesn't exist. Since you haven't created an object, the non-static method doesn't exist yet. A static method (by definition) always exists.
combined with above answer by Mathew Brown
If you're already in a non-static method, you've already got a suitable object, which is the one represented by this. Calling show() is equivalent to calling this.show().
Joined: Aug 31, 2012
Ok now i understood.
Until now i thought that you can call only static methods from another static method
Well the rule is : You can call non-static method from a static method by creating an object of the class in which the non-static method resides and then the non static method can be accessed using that object.
Here is the complete example:
Joined: Sep 06, 2007
Matthew Brown wrote:
abalfazl hossein wrote:then main method is an exception, Right?
No, the main method is the same as any other static method.
If it isn't, Then give me an example that a static method can use non static method. That static method in this case is not main.Thanks in advance