Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a class with only static methods be abstract?

Tags:

java

I have a class that offers a collection of static utility-type methods.

On the one hand, I don't want the class to be able to be instantiated. On the other hand, I don't want to send the signal that the class should be inherited from (not that I think that it's likely).

Should this class be abstract or not?

like image 591
oadams Avatar asked Feb 15 '13 00:02

oadams


People also ask

Can static class have abstract method?

If you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.

Can abstract class have non static methods?

The only way to access the non-static method of an abstract class is to extend it, implement the abstract methods in it (if any) and then using the subclass object you need to invoke the required methods.

Should static classes be abstract?

Yes, abstract class can have Static Methods. The reason for this is Static methods do not work on the instance of the class, they are directly associated with the class itself.

Can a class have only static methods?

What is Utility Class? Utility Class, also known as Helper class, is a class, which contains just static methods, it is stateless and cannot be instantiated. It contains a bunch of related methods, so they can be reused across the application.


4 Answers

make the class final and make the default constructor private and do not provide any public constructors. that way no one can subclass it or create an instance of it.

like image 169
mre Avatar answered Nov 14 '22 11:11

mre


Don't declare it abstract; declare a private constructor, so no one, not even a subclass, can instantiate an instance of your utility class.

You can declare your class final, although if all constructors are private, then no one will be able to subclass it anyway.

To borrow the idea from Pshemo's comment in another answer, throw a RuntimeException in the constructor to prevent reflection's setAccessible method in AccessibleObject from allowing instantiation:

public class MyUtility
{
   private MyUtility()
   {
      throw new RuntimeException("Instantiation of MyUtility is not allowed!");
   }

   public static void utilityMethod()
   {
      // Your utility method here.
   }
}
like image 35
rgettman Avatar answered Nov 14 '22 12:11

rgettman


Although a top-level class can't be declared static, you can make the class non-instantiable (and practically 'static') to other classes by declaring the default constructor private, which forbids instantiation because no constructor is visible.

like image 40
Robert Harvey Avatar answered Nov 14 '22 10:11

Robert Harvey


Another version of @mre's answer

enum MyClass{
    ;//this semicolon indicates that this enum will have no instances

    //now you can put your methods
    public static void myMethod(){
        //...
    }
}

Enum by default is final and its constructor is private. Also you cant create its instance with reflection because it checks in Constructor#newInstance if you are trying to instantiate Enum object.

like image 21
Pshemo Avatar answered Nov 14 '22 10:11

Pshemo