• 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

Doubt Regarding static reference

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Fub {
public static void m(){ System.out.println("fub"); }
}
public class Foo extends Fub {

public static void m(){
System.out.println("foo");

}
public static void main(String[] args){
Fub f = new Foo();//line 1
f.m(); // line 2
m();
}
}


The above program gives me doubt at line 1 and 2. when you contain the reference of subclass in the superclass reference variable then the variable should point to the method of the subclass as done in method overriding. But here its pointing to the superclass method. So I doubt whether it has anything to do with the declaration of methods as static. Please clarify
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the same doubt..can someone explain please
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here, you have declared static method means, you should always think about reference not object...
Now, in Fub f = new Foo(); //line 1 , you have created an object of type Foo but reference is of super class... Just told, think about reference... f.m(); will call super class static method...
Next is m(); ... no reference... so overridden static method will be called...
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In short, static methods cannot be overridden.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While it comes to Static methods static methods are never get inherited. So that mean you cannot override it, then what it comes to defining it in the subclass is termed it as "REDIFING".

So the Dynamic Method Dispatch wont work when i comes to Static Methods.

And it depends on the type of instance rather than the type of Object.

Hope it clears a bit.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are not inherited .
Method to be invoked is resolved at compile time .
Method is invoked according to the actual reference type
not as actual object type .
 
ansuman mohapatra
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes that helped...thanks
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ronaldo,Prasad,Madhukar

I want to clarify/"get clarified" on the topic applicability of Inheritance, Polymorphism to static methods/variables.

You say static methods cannot be inherited thereby cannot be overridden.
I suppose this is not the case. Just compile and run the following code.


Inheritance applies for Static methods/variables.
When it comes to Polymorphism, here is a snippet from K & B book, page 98

Polymorphic method invocations apply only to instance methods. You can
always refer to an object with a more general reference variable type (a superclass
or interface), but at runtime, the ONLY things that are dynamically selected
based on the actual object (rather than the reference type) are instance methods.
Not static methods. Not variables. Only overridden instance methods are
dynamically invoked based on the real object's type.


I suppose you agree with me.
reply
    Bookmark Topic Watch Topic
  • New Topic