Apart from the use mentioned in Static Keyword In Java, there are other uses of
static keyword. They are creating the constants, initializing them using static block and creating static methods.
Constants: The
static variables of any class can be marked as final , so that they become constant or unmodifiable. Declaring the constants as public static final is a very common practice.
public static final double PI = 3.14; Static Blocks: We can use static blocks inside a class to initialize static variables or do any one time activities, we want to perform before the class is used.
class Temperature static blocks as needed and they will be called in the same order as they appear in the file. Also note that they will be called only once, no matter how many objects are created or how many times the class is accessed.
Static Methods: In any class, we can also define static methods. Similar to static variables, we can access static methods, using the class name and do not need any object for calling them. Usually we group utilities which are used across the application and create static methods for them. When we create static methods as shown below, we need not create unnecessary new objects, every time we want to make a conversion.
class ConversionUtils
Dependent Topics :
|