wood burning stoves*
The moose likes Beginning Java and the fly likes Problem with Objects and Method Call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Objects and Method Call" Watch "Problem with Objects and Method Call" New topic
Author

Problem with Objects and Method Call

Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
I have following two classes:




Then as expected it gives me an error something like non-static method show() cannot be referenced from a static context


But when i change show(); to
Then it compiles and runs fine.My question is that why it ran sucessfully 2nd time? It is still calling non static method show from static method main().
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ganesh Pat
Ranch Hand

Joined: Feb 04, 2012
Posts: 79

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...
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
@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?

@Ganesh Pat:
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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

obj is the context.
Ganesh Pat
Ranch Hand

Joined: Feb 04, 2012
Posts: 79

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...
Ganesh Pat
Ranch Hand

Joined: Feb 04, 2012
Posts: 79

and main is public so from anywhere you can access it..
Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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 ???
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
1- a static method, can only call static method.

2-main is static

3-Now, How can main call show()?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

You're not understanding the restriction.

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!
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
then main method is an exception, Right?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

abalfazl hossein wrote:then main method is an exception, Right?

No, the main method is the same as any other static method.
Ankit Dan
Ranch Hand

Joined: Aug 31, 2012
Posts: 47

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().
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
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:






abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

abalfazl hossein wrote:
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

So, you are actually saying that: - "No other static method other than main, can create an object of a class and call it's non-static method"??

Take a look at this code: -
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
abalfazl hossein wrote: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

See Rohan's StaticExample and StaticSecond classes above.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Objects and Method Call