• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Static methods compiler question

 
Author
Posts: 134
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason,
.

[ April 26, 2007: Message edited by: Srinivasan thoyyeti ]
[ April 26, 2007: Message edited by: Srinivasan thoyyeti ]
 
Jason Porter
Author
Posts: 134
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic