This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

static method confusion  RSS feed

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am reading chapter2 of K&B and on pg.147 it says that
static methods can't be overriden but they can redefined.

package chapter2;

class StaticParent{
static void doStuff()
{
System.out.println("static parent");
}
}

public class StaticExampleTest extends StaticParent {

static void doStuff(){
System.out.println("StaticExampleTest");
}

public static void main(String[] args) {
StaticParent[] parent = {new StaticParent(),new StaticExampleTest(), new StaticParent()};


for( int x = 0 ; x < parent.length ; x++){
parent[x].doStuff();
}

}
}

When i run the above program i get output as

StaticParent,StaticParent,StaticParent.

If i say, in the array, one instantiation is for the object
of StaticExampleTest as well which extends StaticParent, then
why the version of StaticExampleTest wasn't printed.

Is doStuff() in StaticExampleTest is its own class method or
inherited from its Parent class? I am very confused about this.
Please help.

Ben
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods are inherited but can't be overridden.
There is no any overriding taking place...............
[ September 08, 2008: Message edited by: ARIJIT DARIPA ]
 
Arijit Daripa
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Arijit Daripa
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods are inherited
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by ARIJIT DARIPA:
static methods are inherited


That's not an instance of inheritance, that's an instance of accessibility. Since both classes are in the same package, the static method is available to the Child subclass. Since Child is castable to Parent(), when the StaticMeth method is attempted to be invoked, the compiler first checks for a method of Child that matches it. It does not find that, so it checks the super class. It does find a method, but it is static. So, it uses the static shortcut. Saying c1.StaticMeth is equivalent to ((Parent)c1).StaticMeth(); This shortcut is then taken one step further and the result is Parent.StaticMeth(); Only non-static methods and variables are inherited, statics by definition cannot be inherited because they are only defined for the class or interface they are declared in. Just because you can access them from this sort of shortcut doesn't change this, it is a convienience, nothing more.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben, I think what needs to be noticed here is that, in your test program, your array reference was of type "parent" ( StaticParent[] parent = ).

When you assigne the three objects you set the array to one StaticParent, then one StaticExampleTest and finally one SaticParent. You then step through the array calling the method on each object. Now HERE is the important part.

Static methods are not inherited, per se. They can be replaced/substitiued. The thing to remember is that you get the method that goes with the reference type!!! So since your array type was StaticParent then, even thought the[1] object was a StaticExampleTest itself, since the reference is StaticParent, StaticParent's version of the method will be called.

Try this:



and see what happens.

This is like this because "a static method belongs to the class". In other words, when you have a Superclass - Subclass relationship between two classes and each class has a static method with the exact same name and signature, you get the version that goes with the type of the reference. Recall that a sataic can be called in one of two ways:

1) by the class (StaticParent.doStuff(); ) , or

2) by an instance reference, such as in your test code. But the version that you get is tied to the type of the reference, in the case of a static method.
[ September 09, 2008: Message edited by: Bob Ruth ]
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob is rite..... superclass object referencing is not letting your code to access the child class methods......!!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!