aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are these statements true? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Are these statements true?" Watch "Are these statements true?" New topic
Author

Are these statements true?

Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
When we have a number of classes forming a inheritance chain then the
appropriate overridden method is determined by the run time type of the object
where as if we consider static methods or fields then the appropriate one to be
executed will be decide by the reference type.


A static method in a super class can be overridden by another static method in
subclass.

Are these statements true???


Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
if you have a static method in your class, that method can only be called by using the class name it is from, the dot operator and the method name. Ex. Math.min(int x, int y); //the min(int x, int y) method is static. Class Math has a private constructor so no new instance of a Math class can be created.


I'm a Hood Ornament
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Yes (see "polymorphism") and no (static methods cannot be overridden).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Megs]: if you have a static method in your class, that method can only be called by using the class name it is from, the dot operator and the method name.

No. There are at least three other ways to call a static method. I will leave this as an excercise, for now.

Going back to the original question:

[Nilesh]: When we have a number of classes forming a inheritance chain then the appropriate overridden method is determined by the run time type of the object where as if we consider static methods or fields then the appropriate one to be executed will be decide by the reference type.

Correct - assuming you are using a reference to invoke the method.


"I'm not back." - Bill Harding, Twister
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Megs Maquito:
if you have a static method in your class, that method can only be called by using the class name it is from, the dot operator and the method name...

This is how a static method should be called, to make it clear that the method is static. However, a static method can be called using a particular instance in place of the class name.

(Watch for this in the SCJP exam. In particular, note that Thread's static sleep method can be called through any Thread instance, but it acts on the current thread.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are these statements true?