my dog learned polymorphism*
The moose likes Beginning Java and the fly likes an object can call static method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "an object can call static method." Watch "an object can call static method." New topic
Author

an object can call static method.

Ramakrishna Konanki
Greenhorn

Joined: Apr 18, 2008
Posts: 20
in the site mock example i have seen code
like
an object can call static method.


as i've studied static members should be called with class name.

please justify the solution
Harnoor Singh
Ranch Hand

Joined: Aug 24, 2010
Posts: 35
Lets us say we have class with name Har which has static method with name stat. We can call stat by following 2 ways :-

1)Har.stat();
2)obj.stat();--> where obj is object of class Har

Though both are allowed by compiler , but option 2 is discouraged.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Even though you invoke the method with the object, the compiler implicitly do the rest!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

An static method can be called by a object,there is no problem but the code will compile with warning saying
static method should be accessed in a static way




SCJP6.0,My blog Ranchers from Delhi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
"Ramakrishna" you have been warned before about your displayed name. Please read this.
 
Don't get me started about those stupid light bulbs.
 
subject: an object can call static method.
 
Similar Threads
Static synchronized methods
overriding
calling main
JMock with Static Object
Using GregorianCalendaer class