The static keyword can be applied to fields, methods, and classes
- When you declare a variable inside a class, it is declared as a member variable and must be accessed through an object like
car.name - However, if you declare a variable using the static keyword, it can also be
accessed through the Class- When accessed through the Class, it can be
accessed externallywithout creating an object - It 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!
- Why?
- When accessed through the Class, it can be
- Variables declared as static are created and initialized
when the program is executed- Therefore, even without creating an object, the
variable is already in a created state!
- Therefore, even without creating an object, the
- Declaring as
final staticmakes it aconstantwhose value cannot be changed- Conventionally, to easily distinguish static constants, names are written using only
uppercase lettersand underscores (_)
- Conventionally, to easily distinguish static constants, names are written using only
Access modifiers- When declaring a static field, you can set the scope you want to expose using access modifiers
Reasons for use- Most static fields are used when
declaring constants- When you declare a constant with the
final statickeyword, other classes can also access the constant - It 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
- When you declare a constant with the
- Declaring too many variables as static moves away from object-orientation and can easily lead to spaghetti code (complex tangled code)
- Most static fields are used when
- 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 Class Cautions- A static method can be called without creating an object, which means
the method is separated from the object- Therefore, keywords like
superandthiscannot be usedinside the method, - and it also
cannot accessthe class'smember variables
- Therefore, keywords like
- In static methods, only variables declared inside the method, static fields, and static methods can be accessed
- A static method can be called without creating an object, which means
Reasons for use- Static methods are typically used to create
UtilsandHelperclasses-
ex) Creating a Math Utils class
public class Math { public static double sqrt(double num) { // .... } public static double sum(double num1, double num2) { // .... } }
- It has the advantage of grouping math-related methods under a class called Math (
grouping)
- It has the advantage of grouping math-related methods under a class called Math (
-
- Static methods are typically used to create
-
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 variables- Declaring 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 createdCar.Wheel wheel = new Car.Wheel();
-> This is the biggest difference between
Inner classandStatic nested class
-
-
Cautions- Static class is only possible
when declaring a sub-class- Trying to declare the upper class as static will cause a compile error
- Static class is only possible
-
Reasons for using
static class- The benefit of creating an Inner class as a static class is
grouping- There 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!
- This can also be done with Inner classes, but Inner classes are
- There is an advantage of being able to group related classes by declaring classes associated with a certain class below it
- The benefit of creating an Inner class as a static class is
- The common aspect of the static keyword is
separation from objects- It allows access without creating an object!
- It has the advantage of
Grouping- Methods and classes related to a certain class can be declared as static below it to gather them in one place!!