• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can a static method be final?

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai ranchers,
Please see the code below. This is from jiris mock - 3.
What is the output of trying to compile and run the following code?
(Select one correct answer)

Lines 1,2 & 3 are pretty clear. But what about line 4. I read a hundred times that static methods are inherited but not overridden. They are just hidden by subclass's implementation.
If this statement is true how come a static method which doesn't participate in overriding can be final. Isn't this illegal. I tried extending this class and redifing the static method but it failed as I expected.
I'm totally confused. someone please explain.
Cheers
-Suresh
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think static and final have different meanings... static means it is defined for a class and final is something that cannot be overridden.So it is legal to have a method defined as both static and final
pls explain on why u think it strange to have it
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a method is declared as static,the method is inherited but not overridden-thats true.But this applies only for polymorphism.If a base class method is marked final,even the slightest attempt to override(or as u say hide the implementation of the base class method by providing a new method with the same argument list and name in the sub class)will make the compiler angry.
ie: The static methods in the base class marked as final cannot be overridden,or in other words a method with same name and argument list cannot exist in the sub class.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that a static method cannot be overridden. I think its only that static defines a method to have a class scope. like the following piece of code is ok....
class testrun
{
boolean tell;
static void foo(){}
}
class test2 extends testrun
{
boolean tell;
static void foo(){ System.out.println("I am Amit");
}
Its only that the static method of the super class has to be declared static in the subclass too.
Its only the keyword final that is the one that prevents overriding the method. so adding final will not let you override foo in the class test2.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
1. static methods can not be overidden they are only hidden so when you give the same signature of method in derived class as in the base class then that method is hidden only no polymorphism takes place it is type of reference whoes method will be called compiler will not check to which class this reference is pointing to at runtime.
2. final methods cannot be overridden.
3. when the method is static and final it means that you cannot even give the same signature of the method in derived class as in base class.
In this example
class testrun
{
boolean tell;
static void foo() // No final is given
{
System.out.println("Hi inside base class");
}
}
class test2 extends testrun
{
boolean tell;
static void foo()
{ System.out.println("I am Amit");
}
public static void main(String args[])
{
testrun t = new test2();
t.foo();

}
}
So output: Hi inside base class
and when you try to do something like this
import java.io.*;
class testrun
{
boolean tell;
final static void foo()
{
System.out.println("Hi inside base class");
}
}
class test2 extends testrun
{
boolean tell;
static void foo()
{ System.out.println("I am Amit");
}
public static void main(String args[])
{
testrun t = new test2();
t.foo();

}
}
the compiler will complain that final method can not be overridden.
Hope this will clear your doubts.
Regards
Rashi
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add more to what Rashi mentioned,
Static methods can't be overriden. All the examples that are shown on this thread are not really trying to override. Sub class static method was shadowing the super class method.
If you try the following example, you will understand the Compiler issues

I hope this makes it clear
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, static methods can not be overridden. If a subclass defines a static method with an identical signature, the method inherited from the parent class is said to be "hidden." I just posted a rather lengthy discussion about this in my blog. Feel free to check that out.
However, regarding the use of the keyword final, the JLS, in section 8.4.3.3 final Methods states:


A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.


As you can see, the keyword final pertains to both overriding AND hiding. Therefore, you can declare a static method to be final to prevent subclasses from hiding that method.
I hope that helps,
Corey
 
Suresh Thota
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.


Corey, that's what am exactly looking for. Thanks a lot. am too lazy to go through JLS and at times like this I feel bad for that.
Thanks to all for the nice discussion.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Suresh Thota:

am too lazy to go through JLS and at times like this I feel bad for that.


I know that reading through the JLS can be a chore, especially when you're not used to it. However, there is not better source for SCJP exam information than the JLS. Force yourself to wade through it and, after a while, you'll get a knack for reading it. Once you've made it that far, it's just like looking in any old book for the information you need.
Corey
 
reply
    Bookmark Topic Watch Topic
  • New Topic