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


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "static overloading" Watch "static overloading" New topic
Author

static overloading

Matiz Dave
Greenhorn

Joined: Sep 13, 2008
Posts: 6
I have the following code


When I run TestStaticOverloadingChild class, it prints
parentm1
childm2

I know that static methods are decided at compile time. However would method m3 be not inherited in this case and should instance of TestStaticOverloadingChild not call m1 of TestStaticOverloadingChild class?

Thanks in Advance
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Dave,
Static methods cannot be overridden. Only method m2 is overridden in the Child class.
When you run the code, you invoke m3() on the object of child but since its doesnt have that method, it will invoke the method in Parent(as it extends Parent)..so your invoking Parents m3() from which there's a call to m1(). m1() is static and belongs to the class. Invoking m3() of Parents executes m1() of Parent only. (Try overridding m3 in Child & and you can see the difference). But when invokation of m2 comes, it will execute Child's method as that method is overridden in the Child.

Try compiling and running the code by removing "static" from m1 in both class. you will see the change.


SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16815
    
  19

I know that static methods are decided at compile time. However would method m3 be not inherited in this case and should instance of TestStaticOverloadingChild not call m1 of TestStaticOverloadingChild class?


Inherited doesn't mean that the child class gets a copy of the method, if it doesn't override. It just means that at runtime, the parent's version of the method will be called -- because it didn't override.

The parent version of the method, has already been compiled to called its version of that static method. It doesn't know that the child has its version of the static method.

[EDIT: And maybe the title should be renamed -- as this question is about calling static methods from an overridden (or non-overridden) methods. And not about overridding static methods.]

Henry
[ September 13, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
static methods are NOT overridden. Hence, m1 () of the parent class is not overridden in the child class. Also, which overridden method to call is dependent on the object active during runtime.

Based on the above: "new TestStaticOverloadingChild()" refers to the child class's object and hence:
(1) the non-static method m2() which can be overridden is called from the child class.
(2) the static method m1() which cannot be overridden is called from the parent class.
Matiz Dave
Greenhorn

Joined: Sep 13, 2008
Posts: 6

Inherited doesn't mean that the child class gets a copy of the method, if it doesn't override. It just means that at runtime, the parent's version of the method will be called -- because it didn't override.


I am calling method m3 from an instance of TestStaticOverloadingChild and thought that it would get an implict access to m3 as if the method belonged to TestStaticOverloadingChild. Does it mean that at runtime, jvm maintains a copy of each object up the heirerchy? Or only one object is created but the object has information about all the objects which this object has inherited from?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16815
    
  19

Originally posted by Matiz Dave:

Does it mean that at runtime, jvm maintains a copy of each object up the heirerchy? Or only one object is created but the object has information about all the objects which this object has inherited from?


For methods, neither description is completely correct. Method definitions are *not* part of the instance -- it is really expensive to make copies of every method for each instance.

Java does have the class definitions for all the classes up the heirarchy. And the instance does point to them. But there is no copy of each of the objects. It is one object with the correct jump tables to the correct class objects which have the methods.

Henry
Matiz Dave
Greenhorn

Joined: Sep 13, 2008
Posts: 6
Thanks a lot
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12953
    
    3

Rekha, please check your private messages. You can see them by clicking My Private Messages.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: static overloading
 
Similar Threads
Instance Variable
synchronization
what really it mean's
preculiar synchronized behaviour
what does it stand for >>>>reference variable.reference variable