How to create a class that is not inherited and static in java?

Could you help me create a class that is not inherited and must be static in behavior (which means we cannot instantiate it). I need this class to hold constant values. Thanks.

+4
source share
8 answers

you can use the final keyword for a class that cannot be inherited. But I would recommend you use Enums . Enums cannot be inherited, and only one instance exists for a constant value. You can also do much more with Enums .

 public enum DaysOfweek { SUNDAY,MONDAY..... } 

Read more about Enums here. Enum types

+6
source
 public final class MyClass { private MyClass() { } } 

The final keyword makes it non-inheritable, and the private constructor will help stop its creation.

+7
source

Why not use an interface that has a couple of static and final variables?

+4
source
  • In the first case, you can use the final class, since they cannot be inherited.
  • For your second case, you can use interface , they are very well used for storing Constants .

But you cannot have both of them together (since having an interface that cannot be implemented does not make sense) ..
So, best of all, you can mix the property as in one class. You may have a final class variable with public static final , since this is what the constant variable in the interface does, will serve the purpose of Constants ..

 public final class A { public static final int YOUR_CONST = 5; } 

If you do not want to instantiate this class, you can have a private 0-arg constructor in it.

 public final class A { public static final int YOUR_CONST = 5; private A() {} } 
+3
source

Declare it as final, which will not allow other classes to extend it and make its constructors private, so no one else (except your own class) will be able to create it:

 public final class MyClass { private MyClass() { // Private so noone else can instantiate this } } 
+2
source

The following is an example of a class containing a constant value:

 public final class Trial // it is the FINAL { private static final int CONSTANT_VALUE = 666; private Trial() // it is PRIVATE instead of PUBLIC { } public static int getConstantValue() { return CONSTANT_VALUE; } } 

And below is an example of how to test the above class:

 public class Bully //extends Trial ////"extends" WILL NOT COMPILE { public static void main(String[] args) { //Trial trial = new Trial(); ////"new Trial()" WILL NOT COMPILE // The only thing can be done is getting a constant value from "Trial" int acquiredValue = Trial.getConstantValue(); System.out.println(acquiredValue); } } 

Hope that helps :))

+2
source
 public final class MyClass { public static string MY_STRING; public static int MY_INT; private MyClass() {} } 
+1
source

final: Make a final class, which means that this class can no longer be inherited. static: if all methods are static, you do not need to instantiate this class. and should be static in behavior.

Syntax pattern: if all your methods are not static, and you do not want to create more than one instance, you can make the constructor private and save one variable of the class object in the class. And create once if it is null, and if not null, then always return the same instance.

thanks

+1
source

Source: https://habr.com/ru/post/1437104/


All Articles