Static Keyword
The static keyword can be applied to fields, methods, and classes
Static Field (Variables, Constants)
When you declare a variable inside a class, it is declared as a member variable and must be accessed through an object like
car.nameHowever, if you declare a variable using the static keyword, it can also be
accessed through the ClassWhen accessed through the Class, it can be
accessed externallywithout creating an objectIt can also be accessed through an object, but accessing through the Class is recommended
Why?
Because it is difficult to distinguish whether you are accessing a member variable or a static variable!
Variables declared as static are created and initialized
when the program is executedTherefore, even without creating an object, the
variable is already in a created state!
Declaring as
final staticmakes it aconstantwhose value cannot be changedConventionally, to easily distinguish static constants, names are written using only
uppercase lettersand underscores (_)
Access modifiersWhen declaring a static field, you can set the scope you want to expose using access modifiers
Reasons for useMost static fields are used when
declaring constantsWhen you declare a constant with the
final statickeyword, other classes can also access the constantIt can also be declared as a member variable, but implementing it this way means each object holds the constants, which can increase the object's size
Declaring too many variables as static moves away from object-orientation and can easily lead to spaghetti code (complex tangled code)
Static Method
Static methods are similar to static fields
If you use the static keyword when declaring a method, you can call that method without creating an object
Since it is difficult to distinguish whether you are calling an object method or a static method, it should
only be called through the ClassCautionsA static method can be called without creating an object, which means
the method is separated from the objectTherefore, keywords like
superandthiscannot be usedinside the method,and it also
cannot accessthe class'smember variables
In static methods, only variables declared inside the method, static fields, and static methods can be accessed
Reasons for useStatic methods are typically used to create
UtilsandHelperclassesex) Creating a Math Utils class
It has the advantage of grouping math-related methods under a class called Math (
grouping)
Static Class
Declaring a class using the static keyword separates it from the upper class
An
Inner classisconnected to the upper class, so it can access the upper class's member variablesDeclaring a sub-class as static makes it impossible to access the upper class's member variables!
A
sub-class declared as staticis called astatic nested class
A
Static nested classcan have objects created directly from the outside even without the upper class being created-> This is the biggest difference between
Inner classandStatic nested class
CautionsStatic class is only possible
when declaring a sub-classTrying to declare the upper class as static will cause a compile error
Reasons for using
static classThe benefit of creating an Inner class as a static class is
groupingThere is an advantage of being able to group related classes by declaring classes associated with a certain class below it
This can also be done with Inner classes, but Inner classes are
connectedto the upper class and are not independent classes!
Summary of Static Keyword Advantages
The common aspect of the static keyword is
separation from objectsIt allows access without creating an object!
It has the advantage of
GroupingMethods and classes related to a certain class can be declared as static below it to gather them in one place!!
Last updated