• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Whats the Difference

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

what is the difference between

A static method

and

A static final method
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods belong to the class and you can have the same method defined in the subclass. (Static methods are not overridden and when defined in the subclass, they appear are completely new method)

Final static methods also belong to a class but then you cannot have a method with same name in the subclass.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a method, say, "static void Method()" in my parent class and if I override it as "void Method()" in my subclass, the compiler screams at me saying "...cannot override...overridden method is static"...

I am forced to put the static keyword in the subclass also and I believe its considered to be an entirely new static method of the subclass.

Is my understanding correct? And is it ok to do in such a way? I mean, is polymorphism compromised in any way here, because of the compulsory addition of "static" keyword in my sub class method?

Refer to this link as well.
https://coderanch.com/t/270416/java-programmer-SCJP/certification/static-variables-methods
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i know, Static methods cannot be inherited, so there is no question of Overriding.

According to me, it is same in case of methods.

But in case of variables, static final indicates that the variable belongs to class(in other words, there is only one copy of the variable which is shared by all the objects of that class) and its value cannot be changed.

While only static indicates, that it belongs to class and its value CAN be changed.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chander shivdasani:
[QB]As far as i know, Static methods cannot be inherited, so there is no question of Overriding.[/QB]



Static methods can be inherited, though they can not be overridden.


[ September 28, 2008: Message edited by: Aravind Jerubandi ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When you have a static method in super class, then you can override only if overridden method is static, for eg
class A {
public static void m1(){}
}
class B extends A{
public static void m1(){}// OK
}

but this is not OK

class A {
public static void m1(){}
}
class B extends A{
public void m1(){}// Not OK
}
because method in superclass is static and is not inherited.

So when method is final static in superclass, then you cannot override the method in subclass even after making it static like this
class A {
public static final void m1(){}
}
class B extends A{
public static void m1(){}// Not OK, method is final
}
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't want to derail too much from the original question, but just point out this is another example of where the language of object-orientation is not perfectly well defined: Some people say that static method are inherited, while other people say that's not inheriting. If you want to be on the safe side, you might simply say that they are available in the subclass.
 
Manju Kavi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that correct, they are available in the subclass. But the polymorphism will not work.

class A {
public void m1(){
System.out.println("SUPER");}
}
class B extends A{
public void m1(){
System.out.println("SUB");}
}

then,
A a=new B();
a.m1; // print "SUB"

but when methods are static, then
a.m1; // print "SUPER".. No Polymorphism here
 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manju has told very true.

We cant override/redeclare a final static method in the subclass.
But why so, when we cannot override a static method then why cant we redeclare it?
 
Manju Kavi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thats a good question..
when we cannot override a static method then why cant we redeclare it?

But to my knowledge i think its the only way to avoid the subclasses from having the static method with same name as super class (forget overriding as statics are not inherited).
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i know, Static methods cannot be inherited, so there is no question of Overriding.
i.e.
class A{

public static void test(){
System.out.println(" A");
}

}

class B extends A {

public static void test(){
System.out.println(" B");
}
}

public class C{
public static void main(String arg[]){
{
A a=new B();
a.test();
}
}

[1]output:A // since Static method cannot overriding[ But not any error ] so runtime it refer leftside reference varible and call [A a]it's merhod.
[2] if you remove static then output: B// since not Static method overriding so runtime it refer Rightsidereference and call [new B()]it's merhod.
[3] if we use final key word without static pr with static then you can not create any same name method i.e. cann't overriding[Compiler error].

Ihope it's helpful for you
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its strange that the thread went so long and nobody actually mentioned of static methods being hidden when inherited.
The SCJP FAQ contains a very nice article on this.
Static methods can be inherited but cannot be overriden as-in-like polymorphism wont work on them. Thats why they are said to be hidden.
If anybody wants details they can search on the FAQ.
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic