aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can a static method be final? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can a static method be final?" Watch "Can a static method be final?" New topic
Author

Can a static method be final?

Suresh Thota
Ranch Hand

Joined: Sep 24, 2003
Posts: 152
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


SCJP 1.6
Deepa Guha
Ranch Hand

Joined: Mar 12, 2004
Posts: 33
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
Ashok C. Mohan
Ranch Hand

Joined: Dec 03, 2003
Posts: 75
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.


SCJP 1.4
Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment.
Amit Ghai
Ranch Hand

Joined: Mar 24, 2004
Posts: 35
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.


Reasonable people adapt themselves to the world. Unreasonable people attempt to adapt the world to themselves. All progress, therefore, depends on unreasonable people.
Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
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
Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
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


PMP CSQA SCJP SCWCD SCBCD INS 21 INS 23
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Suresh Thota
Ranch Hand

Joined: Sep 24, 2003
Posts: 152

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

Joined: Dec 20, 2001
Posts: 3271
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a static method be final?
 
Similar Threads
spot the difference: public static final
transient ????
Q on modifiers: transient field in abstract class
Doubt in StackOverflowError
Parameter Use