Objective

On this section we will be dealing with static method and variables. The keyword static is one of the modifiers of Java language that is useful when you want to have your variables that is common to all objects.  Maybe you have already encountered the term static fields or class variables. Well, they are simply fields that have static declaration in their declaration.

Static Variable

Below are some important notes on static variables

  • Static variables are associated with the class which means it does not belong to any Object
  • Each instance of the class shares a class variable which is one fixed location in memory thus any object can change the value of the class variable
  • Even though any object can change the value of the class variable, a class variable can be changed or manipulated also without creating an instance of the class.

Static Method

Below are some important notes on static method

  • Static variables are associated with the class which means it does not belong to any Object
  • Each instance of the class shares a class variable which is one fixed location in memory thus any object can change the value of the class variable
  • Even though any object can change the value of the class variable, a class variable can be changed or manipulated also without creating an instance of the class.

Static Variable and Method Example

Sample Output

If you run the above example, we would be having the following output