• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Objects and Method Call

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 79
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
obj is the context.
 
Ganesh Pat
Ranch Hand
Posts: 79
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and main is public so from anywhere you can access it..
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then main method is an exception, Right?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 47
MyEclipse IDE Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: -
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic