File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

an object can call static method.

Ramakrishna Konanki

Joined: Apr 18, 2008
Posts: 20
in the site mock example i have seen code
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 :-

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

Joined: Oct 13, 2005
Posts: 46349
"Ramakrishna" you have been warned before about your displayed name. Please read this.
I agree. Here's the link:
subject: an object can call static method.
It's not a secret anymore!