Answers

Question and Answer:

  Home  Java Game Developer

⟩ What is the meaning and the use of the static keyword in java?

Static is a commonly used keyword, which is associated with variables, methods and nested classes in java. The real use of the keyword is for memory management of these constructs, as detailed next.

1)Static variables in java

When a variable is created as static in Java, it gets memory allocated to it only once, and that too when the class in which it is declared gets loaded. This is the reason why these variables are used to declare properties of other variables which are shared amongst them and do not change.

2)Static methods in java

Methods in java can also be declared as static, and as such, these are then connected to the entire class, without being limited to any single object of the class. Invocation of such a method declared as static does not require the instance of the class and these methods can work on the static data of the class and can even make updates to the value of static constants and variables.

This is also the reason why the main function in java is by default static and hence no object is required to invoke it and this leads to a better memory management.

 147 views

More Questions for you: