Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Objects and Method Call

 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
obj is the context.
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and main is public so from anywhere you can access it..
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1- a static method, can only call static method.

2-main is static

3-Now, How can main call show()?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then main method is an exception, Right?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Eclipse IDE Java MyEclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic