How to use static keyword in Java
The static keyword in Java is a non-access modifier (keywords used to provide additional functionality such as synchronization etc), used primarily for memory management. The keyword static is used to refer to the common property of all objects.
The static keyword can be applied to variables, methods, blocks, and nested classes:
1. Static variable
If a variable is static, the variable is assigned memory once and all objects of the class access the same variable.
A static variable can be created by adding the static keyword before the variable during declaration.
Syntax
The general syntax for declaring a static variable is:
static datatype variable-name
Example
Consider making a simple class which implements the functionality of a counter by incrementing a variable count by one each time an instance of the class is created.
Using a static variable will create one copy of the count variable which will be incremented every time an object of the class is created.
class Counter {static int count = 0;Counter(){count ++;}public void getCount() {System.out.printf("Value of counter: %d \n", count);}public static void main( String args[] ) {Counter c1 = new Counter(); //count incremented to 1c1.getCount();Counter c2 = new Counter(); //count incremented to 2c2.getCount();Counter c3 = new Counter(); //count incremented to 3c3.getCount();}}
All objects of the Counter class will have the same value of count at any given point in time.
Note:
staticvariables can be created at class level only- A
staticvariable is allotted memory once
2. Static method
Similar to static variables, static methods belong to the class and not every instance of the class object.
Syntax
The general syntax to create a static method within a class is:
public class foo {public static void foo2(){//code}}
Example
Consider making a Citizen class which contains a static variable country. We can update all instances of the static variable using a static method setCountry(). Execute the following code to see how this works.
class Citizen {static String country;Citizen() {country = "Pakistan";}public static void setCountry(String c) {country = c;}public void getCountry() {System.out.printf("Current country: %s \n", country);}public static void main( String args[] ) {Citizen c1 = new Citizen();Citizen c2 = new Citizen();Citizen c3 = new Citizen();c1.getCountry();c2.getCountry();c3.getCountry();Citizen.setCountry("USA");c1.getCountry();c2.getCountry();c3.getCountry();}}
Note:
staticmethods cannot use this or super keywords- Abstract methods cannot be
staticstaticmethods cannot be overriddenstaticmethods can only accessstaticvariables and otherstaticmethods
3. Static blocks
A static block is used for initializing static variables. The static block is executed once when the first object of the class is created.
Syntax
The general syntax of creating a static block is:
static {//code}
Example
The following code will initialize the static variables. Execute the code to see the values.
class Foo {static int a;static int b;static {a = 10;b = 2 * a;}public static void main( String args[] ) {Foo foo = new Foo();System.out.printf("a = %d\t b = %d ", foo.a, foo.b);}}
Note:
- There can be multiple
staticblocks in a class
4. Static nested class
A static nested class is a static class within a class. Static nested classes cannot access non-static data members and methods.
The static nested class can be accessed by the outer class without creating an object of the outer class.
Syntax
The general syntax to create a static nested class is:
class OuterClass {static class InnerClass {//code}}
Example
In the following example, the static nested class can be directly accessed in the main() without creating an object of the outer class.
class OuterClass {static String message = "Hello World!";static class InnerClass {static void getMessage(){System.out.println( message );}}public static void main( String args[] ) {OuterClass.InnerClass.getMessage();}}
Note:
- A
staticnested class cannot access instance variables and methods of the outer class without the object’s reference- A
staticnested class can access allstaticvariables and methods of the outer class- In Java, the outer class cannot be
static
In Java, the main method is always static because it is executed before creating an instance of the class which contains the main method.
Free Resources