aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static method confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static method confusion" Watch "static method confusion" New topic

static method confusion

Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151

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(){

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

for( int x = 0 ; x < parent.length ; x++){


When i run the above program i get output as


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.


--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
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

Joined: Aug 09, 2008
Posts: 142
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
static methods are inherited
Sam Aran

Joined: Sep 06, 2008
Posts: 12
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.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
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 ]

SCJP - 86% - June 11, 2009
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
bob is rite..... superclass object referencing is not letting your code to access the child class methods......!!

I agree. Here's the link:
subject: static method confusion