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

Calling static method of an abstract class

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you call a static method of an abstract class?
Please Explain..
Kindly look at the code below..
public abstract class AbstractStatic {
public static void main(String[] argv) {

System.out.println("Hello. The answer is yes.");

AbstractStatic.foo();

}
public static void foo() {
System.out.println("Hello from foo. The answer is still yes.");
}

}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An abstract class cannot be instantiated, but it is a normal class like any other which you can use to perform static (class-related) computations. Since static methods are related to the class and not to an instance of it, you can invoke static methods of abstract classes as your example perfectly demonstrates.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can. And, you can also call foo() by making an explicit call since you are in a static method of the same class.
However, you cannot do this:
this.foo();
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interesting question Murugan!
To clarify / Confuse to clarify,
> An abtract class cannot be instantiated
because it is an incomplete class and one cannot provide concrete implementations of the class (ie via new operator)
> An abstract class can be compiled and executed
just like any other class.
Is this justifiable in any sense? Because ur class is incomplete?
> But there are enough checks in the java Compiler which doesn't let you the unwanted,
like ...
>abstract methods cannot be static
So there is no way u can access the 'incomplete' portion of ur class without having an instance of ur class. (which u cannot because ur class is abstract). so basically u cannot access that abstract method until u give it a concrete implementation.

> u cannot access the instance methods of ur class without having an instance of ur class. So u cannot access them from the "main" of an abstract class.
> So the only thing u can do is access the static methods from the 'main' of ur abstract class.
and get it to run also.
Interesting!
 
For my next trick, I'll need the help of a tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic