• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Are these statements true?

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes (see "polymorphism") and no (static methods cannot be overridden).
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic