Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a class which is not inheritable and static in java?

Tags:

java

Can you please help me how to create a class which is not inheritable and should be static in behavior (Means we should not be able to create instance of it). I need this class to store constant values. Thanks.

like image 375
Eldhose M Babu Avatar asked Nov 28 '22 14:11

Eldhose M Babu


1 Answers

public final class MyClass { 
    private MyClass() { }
}

The final keyword makes it not inheritable, and making the constructor(s) private will help stopping it from being instantiated.

like image 61
Bhesh Gurung Avatar answered Dec 16 '22 01:12

Bhesh Gurung