File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confuse for static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confuse for static method" Watch "confuse for static method" New topic
Author

confuse for static method

Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi!
look at the follow codes:

Running this code produces the output:
a a a

why the second output is "a",but not "b"?
other question: can static method and variable be inheritd?why?
thanks in advance!


who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
your second question : static methods variables ,not can be inherited but not overridden,insteads redefined.
Sbout 1st question :
Animal []x={new Dog(),new Animal()};
when method is called, which version will be called is decided at compile time,since at compile time ,we have no objject-type info,so its based on refernece type only.
so when x.method() is called, no matter what is runtime of x,but compile time type,reference type is Animal.
you read Polymorphism chapter carefully.This topic is ofcourse bit confusing.
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
Lucky J Verma,thanks for help.
As you say "when method is called, which version will be called is decided at compile time." should this rule fit for static method only?
I have remember,for nomal method(no-static),which method can be called decided on compile time(variable type),and which method can be called is dicided at run time(object type).
Gaurav Gambhir
Ranch Hand

Joined: Mar 04, 2006
Posts: 254
In case of overloading
Which overloaded version of the method to call is based on reference type of the argument passed at compile time

In case of overriding
Which overridden version of the method to call is decided at runtime based on object type

In the above example we have two static methods with same signature in parent and child class, but static methods are not overridden, and they are associated with the class so which method to call will be based to reference type
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111




actually, if you closely look these two methods.
you can find difference in
static void doStuff() (in Animal class..uppercase S)
static void dostuff() (in Dog class..lowercase s)

It's a redefinition not an override

regards
samura
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Hi !!!

This article could be of interest:

Overriding Vs Hiding


SCJP 5.0 | SCWCD 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confuse for static method