File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static methods compiler question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods compiler question" Watch "Static methods compiler question" New topic

Static methods compiler question

Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
I've read all of the info on overriding and hiding with static methods. I still have a question though. If what you really are doing is hiding the method, why does the compiler throw an error if you "override" the method incorrectly? I'll add a post I submitted to the local JUG, just trying to understand it, sorry for beating the dead horse if this has come up and I just missed it.

In my testing it very clearly shows that static methods are inherited and can be overridden. In fact if you override incorrectly the compiler complains. Take the following code for example (I have attached it as well):

class StaticParentClass {
static Object printString() {
System.out.print("Hello from StaticParent");
return null;

class StaticSubTest extends StaticParentClass {
static Integer printString() {
System.out.print("Hello from StaticSub");
return null;

class StaticGrandChild extends StaticSubTest { }

If you do a javadoc for the above classes (showing everything) you will clearly see that StaticGrandChild inherits static Integer printString() from StaticSubTest. I also might be reading the output of javap incorrectly but it definitely looks like the method is inherited:

Compiled from ""
class StaticGrandChild extends StaticSubTest
SourceFile: ""
minor version: 0
major version: 49
Constant pool:
const #1 = Method #3.#10; // StaticSubTest."<init>" )V
const #2 = class #11; // StaticGrandChild
const #3 = class #12; // StaticSubTest
const #4 = Asciz <init>;
const #5 = Asciz ()V;
const #6 = Asciz Code;
const #7 = Asciz LineNumberTable;
const #8 = Asciz SourceFile;
const #9 = Asciz;
const #10 = NameAndType #4:#5;// "<init>" )V
const #11 = Asciz StaticGrandChild;
const #12 = Asciz StaticSubTest;

Signature: ()V
line 19: 0

Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method StaticSubTest."<init>" )V
4: return
line 19: 0


If you change the definition of StaticGrandChild to:

class StaticGrandChild extends StaticSubTest {
static void printString() {
System.out.println("Hello from GrandChild");

The compiler throws the following error: printString() in StaticGrandChild cannot override printString() in StaticSubTest; attempting to use incompatible return type
found : void
required: java.lang.Integer
static void printString() {
1 error

Any thoughts?
--- END EMAIL ---

Thanks for helping me understand.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Jason Porter,

I am sorry my friend.
The error shown in console is not actually due to "overriding mismatch".
Its caused due to "abiguity in hiding".

Now try this:

StaticParentClass spc = new StaticSubTest();
Object o = spc.print();
if( o instanceof Integer )
System.out.println("static methods can't be overriden");
System.out.println("static methods can be overriden");

If you are not satisfied with this; Get back to me

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Jason,

[ April 26, 2007: Message edited by: Srinivasan thoyyeti ]
[ April 26, 2007: Message edited by: Srinivasan thoyyeti ]
Jason Porter
Ranch Hand

Joined: Apr 26, 2007
Posts: 120
Makes sense, of course the compiler could give a better error message, but that can be said about a lot of things the compiler says. Thanks all!
I agree. Here's the link:
subject: Static methods compiler question
It's not a secret anymore!