*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between static and final method 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 "difference between static and final method" Watch "difference between static and final method" New topic
Author

difference between static and final method

Linda Xu
Ranch Hand

Joined: Feb 14, 2000
Posts: 51
What's the difference between static method and final method? Neither of them can be overridden by subclass. Why we need two kinds fo method have the same character?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Static methods can be overriden, but they cannot be overriden to be non-static,Whereas final methods cannot be overriden.
Lucy C
Ranch Hand

Joined: Feb 09, 2000
Posts: 53
Static and final methods are very different things! A static method is a method that's invoked through a class, rather than a specific object of that class. Static methods can only access static variables - they can't use anything that's specific to a particular object. Nonstatic methods (or instance methods) must be called on a specific object and can use the object's instance data.
A final method is just a method that cannot be overridden - while static methods are implicitly final, you might also want to create an final instance method.
Lucy C
Ranch Hand

Joined: Feb 09, 2000
Posts: 53
Static methods can be overriden
Um, no, they can only be hidden by having a static method of the same name in the subclass. You don't get the polymorphic behavior you get with overriding if you hide a static method - for example:
class Base {
public static void aMethod(){
System.out.println ("Base aMethod()");
}
}
public class Baselet extends Base {
public static void aMethod (){
System.out.println ("Baselet aMethod()");
}
public static void main (String [] args){
Base theBase = new Baselet();
theBase.aMethod();
}
}

..if you run this, you get the output from the Base version of aMethod, not the Baselet version, even though the object is a Baselet.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Static methods can be overridden ,but the overriding method cannot be non static. Actually,the method in the subclass hides the one in the superclass. If not overridden, Parent.aMethod() and Child.aMethod() refer to the same method.
Feel free to correct me if I am wrong.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
What happens is that the term override does not have meaning for static methods in Java. Overriding implies dynamic binding and static methods do not participate in that type of binding. They are statically bound at compile type. As Lucy said, they can be hidden instead.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
art mudick
Greenhorn

Joined: Feb 15, 2004
Posts: 8
Which of the following statements are true?

1) static methods do not have access to the implicit variable called this
2) a static method may not be overriden
3) a static method may not be overriden to be non-static
4) a static method may not be overloaded
My answer is 1, 2 and 3. But 2 is not listed. Why? I think static methods cannot be overridden only hidden by the subclass. What should be my choices for the question from exam point of view. Can someone tell me.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Art- the author of this question is wrong, 2 should be an answer. I suspect that this particular question is too subtle for the actual exam anyway.


"I'm not back." - Bill Harding, Twister
Matt Claflin
Greenhorn

Joined: Apr 16, 2000
Posts: 6
Hi. My question with regards to the code above then, is, "why would one ever want to hide a static method in a superclass?". If creating an instance of the subclass Baselet{} and subsequently invoking the static method aMethod() causes execution of aMethod() in the superclass Base{}, why would one want to create the aMethod() method in Baselet{}? Perhaps I'm just not seeing how the aMethod() method in
Baselet{} would ever be executed.
-Matt
Matt Claflin
Greenhorn

Joined: Apr 16, 2000
Posts: 6
Ahhh - never mind. I see it. The choice of static method depends on the class of the reference variable which calls it.
-Matt
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hello all,
Very good flow of discussion. Here is some more thought.
The main point to note here is final keyword for methods is an EXTRA attribute or EXTRA nature of that method THis means both static and instance level can have 'final ' as an ADD_ON tag. static and final are are not connected. That is we SHOULD NOT say both static and final give the same functionality. static/instance level access is different and final functionality is different. 'static' means the method is OWNED by ALL objects created by from that class.
The 'final' comes into picture whether the subclasses can overrid or hide whatever. It stops there. When you put final to a static level method it means it is STILL static and the subclasses CAN'T EVEN hide. Which means the subclass can't have it's own version of static behaviour by hiding it. Simillarly for instance methods. It is an extra rectriction put on it. By adding the 'final' the superclass says ' Hello ! All my decenders! IF you want you have my method as it is! All just leave it. You CAN'T override or hide my rules. Ok.!
So we should not mix-up the final and static behaviours. Try this.
<pre>

class Parent {
static void staticMethod() {}
static final void staticFinalMethod() {}
void instanceMethod() {}
final void instanceFinalMethod() {}
}
class Child1 extends Parent {
static void staticMethod() {}
static final void staticFinalMethod() {} // You can't hide like this.
//You violate the law
void instanceMethod() {}
final void instanceFinalMethod() {}
//You can't override like this
}

</pre>
regds
maha anna

[This message has been edited by maha anna (edited April 17, 2000).]
girish
Greenhorn

Joined: Apr 03, 2000
Posts: 19
Maha- but the question still remains can static methods be overridden.
I think the answer to Art's question should be 1 & 3.
Actually I am confused .Please give detailed explaination as always.
rgds
Girish
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Girish,
I think the qstn by art is already answered by Jim. Please read Jim's post in replying for Art's post context.
Beleive me. I started answering your post in detail. Then It struck me why am I doing this? Does Girish just wants the answer. No, he has to give his explanation first. Then I can reply for him like that. Do you get Maha's idea? Let us discuss first. Then the answers come automatically. Please take it in the right sense. What's your opinion about Art's qstn?
regds
maha anna
girish
Greenhorn

Joined: Apr 03, 2000
Posts: 19
Sorry Anna I broke the tradition.
Ok let us discuss.
According to Jims post , the statement
Static methods may not be overridden is true.
But your code:
/////////////////////////////////
class Parent {
static void staticMethod() {}
static final void staticFinalMethod() {}
void instanceMethod() {}
final void instanceFinalMethod() {}
}
class Child1 extends Parent {
static void staticMethod() {}
static final void staticFinalMethod() {} // You can't hide like this.
//You violate the law
void instanceMethod() {}
final void instanceFinalMethod() {}
//You can't override like this
}
///////////////////////////////////
Here what is the statement:
"static void staticMethod() {}"

in Child1? Is it overriding or hiding.

Morever the compiler error like:
"Cannot override static methods with instance methods "
doesn't it suggest you can override static methods with only static methods.
Actually what I mean is - Is overriding not a subset of hiding.
Hope u understand the question.I think i have messed up many things.
Regards
Girish
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Girish,
Now I know what you want.
Overriding a method means what? When you create an object from the subclass which does the overriding, the re-defined (new) behaviour must be kept intact when the method is called on this object right? In other words , there is a base class and it has a method say, void m1() which prints "I like Java". And there is a subclass which is derived from this parent, and it re-defines the beahviour of this inherited method from base class and say, it prints "No I don't like Java". Also lets assume that this method is instance method. So when you create a physical object using Base b = new Base(); and call b.method(), what it will say. Obviously it is real base class object and you would expect it to say "I like Java" Right? Simillarly when you create like this Subclass s = new SubClass(); and call s.method(), it will behave itself and says "I don't like java". Now lets say we do like this Base b = new Subclass(); Now what do u expect? Since this method is instance method, which is overriden by the subclass, eventhough you see through a mirror and see it's another face of Base class, in case of overriding a method, the true face of the overriden method only will be seen. This is oop. Now also it will say "No I don't like Java';
In order to analyze the static concept, let us take the same case as described above. Let's assume Both Base class and subclass has their own version of static void method(); This means what? Each class as a independent class of its own wants to give a COMMON behaviour for all objects created from them. This means when the subclass redefines the same static void method1(), it DOES NOT OVERRIDE. It hides. In the sense, If you apply the same 2 tests as shown above Base b = new Base(); b.method() WILL call Base class version. Subclass s = new Subclass(); s.method() WILL call subclass version. And you know waht? Base b = new Subclass() WILL NOT CALL SUBCLASS VERSION. IT WILL CALL THE BASE CLASS VERSION of the static method. Seeing a subclass object through a base class mirror and when we see a static method, the MIRROR type's static only will be seen. So the mirror beahves differently.
So do you get the point? If the static method WOULD HAVE BEEN OVERRIDEN we MUST see the subclass version right? Then why we see the static base class method? So static method are NOT OVERRIDEN.
For the compiler part, it does say anything about the 'hiding' IT throw s the same error for both instance and static methods. It DOES NOT DISTINGUISH between hiding and overring when it throws the error But it does its work fine.
regds
maha anna

[This message has been edited by maha anna (edited April 20, 2000).]
girish
Greenhorn

Joined: Apr 03, 2000
Posts: 19
Thanks Anna ,
That really clears my doubt.So static methods are not overridden.
Thanks once again .
Looking forward for same cooperation ahead.
Regards
Girish
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Girish,
Did you really read whole of Maha's above post?.
While writing I did't know this is this big. It didn't put you to sleep right?
regds
maha anna
Nalini Mistry
Ranch Hand

Joined: Mar 21, 2000
Posts: 64
Reading maha's explanation of instance and static "like java dont like java" explanation does this hold true : in case of static methods that are hidden by the subclass, the static method called is always the one from the reference variable class, not the object class?? what if we take it a level or two further down the inheritance heirarchy chain?? 3-4 subclasses hiding the same static method?? if a 2nd tier class is used as the referencing variable for a 4th tier object and the static method is called which one executes??
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Nalini,
If I understand you correctly, by the word tier you mean the depth of the inheritance hirearchy right? ( tier is used in server side processing as different meaning.
When you say
<pre>

BaseClass
+SubClass1
+SubClass2
+SubClass3
+SubClass4

</pre>
Subclass2 refVar = new Subclass4();
After the above statement, when you call refVar.someStaticMethod(), the one which is defined in SubClass2 will be called.
regds
maha anna
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
So nobody can really confirm that the answer to the question is really 1, 2 and 3?
That sucks... or the fact that they (whoever) listed 1 and 3 is what actually sucks. How are you supposed to answer this kind of thing without a doubt?
i.e. this plays on whether override by definition relates to polymorphism.
i.e. 2
if its not polymorphic its not overridden.

I think there would be difference of opinion here... even among language designers... so I reckon this kind of question is just not really suited for the SCJP exam.... I hope. :roll:


<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The JLS refers to "overriding" and "hiding" as two different things. Overriding invoves polymorphism. Hiding does not. Since static methods do not participate in polymorphism they can not be overridden. Static methods are inlined at compile time so they can't participate in polymorphism. Think of it this way... static methods should be called by class name not object name so the whole issue should be moot.
Parent.myStatic();
Child.myStatic();
GrandChild.myStatic();
When you call by class name then the question of hiding/overriding is irrelevant. C# has Java beat here since C# does not permit the calling of static methods with an object identifier.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

Which of the following statements are true?
1) static methods do not have access to the implicit variable called this
2) a static method may not be overriden
3) a static method may not be overriden to be non-static
4) a static method may not be overloaded

1. True, only instance methods have this
2. True, see sample code below:

3. True. According to the JLS:

8.4.6.2 Hiding (by Class Methods)
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.

4. False. A static method may be overloaded:
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
And just to add a little more spice here ...
You can also declare a method to be both static and final. If you do, it is illegal to declare another method with the same name and argument list in a subclass:


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between static and final method
 
Similar Threads
difference bet static and final
Difference between static and final
Spot the difference
Whats the Difference
static block