This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are these statements true?

 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes (see "polymorphism") and no (static methods cannot be overridden).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic