This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods compiler question" Watch "Static methods compiler question" New topic
Author

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.

--- START EMAIL ---
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 "StaticTest.java"
class StaticGrandChild extends StaticSubTest
SourceFile: "StaticTest.java"
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 StaticTest.java;
const #10 = NameAndType #4:#5;// "<init>" )V
const #11 = Asciz StaticGrandChild;
const #12 = Asciz StaticSubTest;

{
StaticGrandChild();
Signature: ()V
LineNumberTable:
line 19: 0

Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method StaticSubTest."<init>" )V
4: return
LineNumberTable:
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:

StaticTest.java:20: 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");
else
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!
 
wood burning stoves
 
subject: Static methods compiler question
 
Similar Threads
Check String for null without error?
how many objects
Integer literals implicit casting
find javac version that compiled my class
i'm bit curious about the concatenated string literals with +