• 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

Static method is implicitly final?

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Is "Static method is implicitly final?" true?
I am not sure ...
Can anyone help?
Thanks
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sean,
I don't think so b'cos final methods cannot be overridden. But if you qualify a method as static then it can be overridden by a method in the subclass. Anyway experts might be having better answers..
My answer if false.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Sean that statement makes sense and is true..coz final methods cannot be overridden and also static methods cannot be overridden.....well,static methods can be shadowed in subclass by a static method but definatelly cannot be overridden so Nijesh,that statement is true....
Harpal
 
sean cee
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The reason I was confused was:
Static method can't be overriden (They only get hidden)
Final method can't be overriden either.
But declaring the same STATIC method as the one in super class is ok.
whereas declaring the same FINAL method as the one in super class is illegal.
I don't know....
can someone clear the mud here???
thanks
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static method can't be overriden (They only get hidden)
Final method can't be overriden either.
But declaring the same STATIC method as the one in super class is ok.
whereas declaring the same FINAL method as the one in super class is illegal.

Sean when you declare a method as final that means DON"T YOU DARE DO ANYTHING TO THIS METHOD.....but when you declare it as STATIC...it acts little diffrent that is though it cannot be overridden but it can be shadowed in subclass,i.e. subclass can have its own version of this method but when a call is made to the method in superclass the shadowing method will not be called infact the shadowed method will be called.....
Hope I did not confuse you further,
Harpal
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sean,
The following code should make things very clear to you....
<pre>
class X {
public static void jout(String s) {
System.out.println(s);
}
static void amethod() {
jout("In X, amethod()");
}
}
class Y extends X {
static void amethod() {
jout("In Y, amethod()");
}
}
class Z extends Y {
static void amethod() {
jout("In Z. amethod()");
}
}
public class Tester{
public static void main (String[] args) {
X xy = new Y();
X xz = new Z();
Y yz = new Z();
xy.amethod();
xz.amethod();
yz.amethod();
}
</pre>
The out put is
In X, amethod()
In X, amethod()
In Y, amethod()
Read my above post and then try this programme...
Hope you got it.........
Harpal
 
Nijeesh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harpal,
Thanks for the explanation. I have one more doubt as well. If for eg., i declare a method amethod() as
class abcd{
final static void amethod(){
System.out.println("Inside amethod");
}
public static void main(String args[]){
amethod();
}
}
I cannot extend the class abcd with the same void amethod() defined inside the subclass(since it is declared as final).
But instead if had defined amethod() as
static void amethod()
It could have been hidden/redeclared in the subclass.
So how can we say that static methods are implicitly final. Hope i am not confusing myself... as well u...
Regards,
Nijeesh

 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nijeesh,
Dear, static methods are impli.... final does not mean that there is no diffrence between final and static methods,when you declare a method as final that means it cannot be overridden at any cost but when you declare a method as static it can be shadowed in subclass but not overridden i.e.the subclass will have a method of same parameter return type but it will be it own version...if a object refrence calls the method of subclass the subclass method will be called,if the obj ref calls method of superclass thje superclass version will be called....
Suppose in the Superclass Constructer there is a call to a static method which is also defined in subclass...here the Superclass static method will be called...not the subclass...
"The word final and static are not interchangeble"
hope it is clear,
Harpal
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nijeesh and sean,
Hope it is clear now ....if not pls let me know will try my best to make it clear.......
Harpal
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'nijesh',
Welcome to JavaRanch.
May I remind you that PROPER NAMES ARE NOW REQUIRED!! Please Read the JavaRanch naming policy for more details.
Javaranch appreciates your cooperation and conformance with this policy.
Ajith
 
sean cee
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ppl.
Static method is implicitly final in that it can not be overriden, right?
Thanks Nijeesh, Harpal
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic