File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling static method of an abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling static method of an abstract class" Watch "Calling static method of an abstract class" New topic
Author

Calling static method of an abstract class

Murgan Sub
Greenhorn

Joined: Feb 06, 2002
Posts: 22
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.");
}

}
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
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();
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling static method of an abstract class