• 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 methods cant be abstract

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgive me if this question is very basic, but I'm new to Java !
I have seen a statement that static methods cannot be abstract, although I can't see why not ! Can anyone explain ?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen a statement that static methods cannot be abstract, although I can't see why not ! Can anyone explain ?
static methods cant be abstract because for abstract methods u have to give the body to that method in the derived class which is extending this abstract class .
now static methods are bind to the class name ,
hence ...
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of having an abstract method is to be able to invoke the method on subclasses polymorphically.
For example:
<pre>
// file: Test.java
class Super {
abstract void foo();
}
class Sub1 extends Super {
void foo() {
System.out.println("Sub1.foo()");
}
}
class Sub2 extends Super {
void foo() {
System.out.println("Sub2.foo()");
}
}
public class Test() {
public static void main(String[] args) {
Test test = new Test();
Sub1 s1 = new Sub1();
Sub2 s2 = new Sub2();
test.callFoo(s1);
test.callFoo(s2);
}
void callFoo(Super obj) {
obj.foo(); // polymorphic call to foo()
}
}
</pre>
In the callFoo() method, the JVM determines the actual type of obj and then calls the appropriate foo method. This happens at runtime and it is called "dynamic binding".
"Static binding" on the otherhand, occurs at compile time. This is how calls to static methods are resolved (thus the name "static"). Static methods do not participate in polymorphic calls.
Michael Daconta's discusses this in his book "Java Pitfalls"
HTH
------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by JUNILU LACAR (edited November 06, 2001).]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM resolves the implementation of a static method at compile-time; hence a static method must have an implementation by that time.
An abstract method by definition has no implementation at compile time.
Now, if a method is declared both static and abstract then (by compile-time) it must have an implementation and yet, must NOT have an implementation. This is obviously a contradiction and impossibility.
Hence, according to the JLS (8.4.3.1 abstract Methods)
It is a compile-time error for a static method to be declared abstract.
 
reply
    Bookmark Topic Watch Topic
  • New Topic