Static Variable Java Example
1. Introduction
This is a static variable Java Example. Static variables
are declared with the static word before the type of variable. The main difference among regular variables is that static variables
are not bounded by any object instances, and are shared among all instances.
static datatype Variable //static variable example static int count = 0;
2. Simple Static Variable Java Example
Static variables
are useful when we need to declare a specific value among all instances. We can access Static variables
in 2 ways:
- By Class name
- By Instance name
Create a class with name Laptop
and paste the following code :
Laptop.java
package com.javacodegeeks; /** * @author Petros Koulianos * */ public class Laptop { //static variable public static String motherboard = "ASUS"; public String cpu ; public String monitor_dimension ; public String storage ; }
Create a second class with name StaticVariableExample1
and paste the following code :
StaticVariableExample1.java
package com.javacodegeeks; /** * @author Petros Koulianos * */ public class StaticVariableExample1 { public static void main(String[] args) { Laptop laptop1 = new Laptop(); laptop1.cpu = "i5"; laptop1.monitor_dimension = "15 inch"; laptop1.storage = "500 gb"; Laptop laptop2 = new Laptop(); laptop2.cpu = "i3"; laptop2.monitor_dimension = "17 inch"; laptop2.storage = "1 tb"; //static variables can be access with Class name or with instance name //preferred way is to refer with Class name System.out.println("laptop1 motherboard:"+Laptop.motherboard+" cpu:"+laptop1.cpu+" monitor_dimension:"+laptop1.monitor_dimension+" storage:"+laptop1.storage); System.out.println("laptop2 motherboard:"+laptop2.motherboard+" cpu:"+laptop2.cpu+" monitor_dimension:"+laptop2.monitor_dimension+" storage:"+laptop2.storage); // The company has change the motherboard Laptop.motherboard = "GIGABYTE"; System.out.println("laptop1 motherboard:"+Laptop.motherboard+" cpu:"+laptop1.cpu+" monitor_dimension:"+laptop1.monitor_dimension+" storage:"+laptop1.storage); System.out.println("laptop2 motherboard:"+laptop2.motherboard+" cpu:"+laptop2.cpu+" monitor_dimension:"+laptop2.monitor_dimension+" storage:"+laptop2.storage); } }
Example Output
laptop1 motherboard:ASUS cpu:i5 monitor_dimension:15 inch storage:500 gb laptop2 motherboard:ASUS cpu:i3 monitor_dimension:17 inch storage:1 tb laptop1 motherboard:GIGABYTE cpu:i5 monitor_dimension:15 inch storage:500 gb laptop2 motherboard:GIGABYTE cpu:i3 monitor_dimension:17 inch storage:1 tb
The above example shows how to declare a static variable
and how to access it.
3. Static Variable in Static Blocks
Static blocks can access static variables
since static variables
are initialized before the execution of a static block.
Update Laptop.java
Class and add the following code:
// static block can access static variables static { System.out.println("motherboard: "+motherboard); }
Now run the StaticVariableExample1.java
Class
Example Output
motherboard : ASUS laptop1 motherboard:ASUS cpu:i5 monitor_dimension:15 inch storage:500 gb laptop2 motherboard:ASUS cpu:i3 monitor_dimension:17 inch storage:1 tb laptop1 motherboard:GIGABYTE cpu:i5 monitor_dimension:15 inch storage:500 gb laptop2 motherboard:GIGABYTE cpu:i3 monitor_dimension:17 inch storage:1 tb
In the above example, we saw that when the static block runs can access and print the static variable
.
4. Static Methods
The static variables
can also be accessed inside static methods.
Update Laptop.java
Class and add the following code:
Laptop.java
package com.javacodegeeks; /** * @author Petros Koulianos * */ public class Laptop { //static variable public static String motherboard = "ASUS"; public String cpu ; public String monitor_dimension ; public String storage ; // static block can access static variables static { System.out.println("static block motherboard : "+motherboard); } //This is a Static Methods access static variables static void motherboardname(){ System.out.println("static method motherboard : "+motherboard); } }
Update StaticVariableExample1.java
Class and add the following code:
StaticVariableExample1 .java
package com.javacodegeeks; /** * @author Petros Koulianos * */ public class StaticVariableExample1 { public static void main(String[] args) { Laptop.motherboardname(); Laptop laptop1 = new Laptop(); laptop1.cpu = "i5"; laptop1.monitor_dimension = "15 inch"; laptop1.storage = "500 gb"; Laptop laptop2 = new Laptop(); laptop2.cpu = "i3"; laptop2.monitor_dimension = "17 inch"; laptop2.storage = "1 tb"; //static variables can be access with Class name or with instance name //preferred way is to refer with Class name System.out.println("laptop1 motherboard:"+Laptop.motherboard+" cpu:"+laptop1.cpu+" monitor_dimension:"+laptop1.monitor_dimension+" storage:"+laptop1.storage); System.out.println("laptop2 motherboard:"+laptop2.motherboard+" cpu:"+laptop2.cpu+" monitor_dimension:"+laptop2.monitor_dimension+" storage:"+laptop2.storage); // The company has change the motherboard Laptop.motherboard = "GIGABYTE"; System.out.println("laptop1 motherboard:"+Laptop.motherboard+" cpu:"+laptop1.cpu+" monitor_dimension:"+laptop1.monitor_dimension+" storage:"+laptop1.storage); System.out.println("laptop2 motherboard:"+laptop2.motherboard+" cpu:"+laptop2.cpu+" monitor_dimension:"+laptop2.monitor_dimension+" storage:"+laptop2.storage); } }
Now run the StaticVariableExample1.java
Class
Example Output
static block motherboard : ASUS static method motherboard : ASUS laptop1 motherboard:ASUS cpu:i5 monitor_dimension:15 inch storage:500 gb laptop2 motherboard:ASUS cpu:i3 monitor_dimension:17 inch storage:1 tb laptop1 motherboard:GIGABYTE cpu:i5 monitor_dimension:15 inch storage:500 gb laptop2 motherboard:GIGABYTE cpu:i3 monitor_dimension:17 inch storage:1 tb
5. Static final variables
It is very common to combine static variables
with the final modifier to declare constants. Keep in mind that by convention final static values are spelled in uppercase letters .
//final static variable public final static String COMPANY ="FOO Company";
6. Static variables and Multithreading
Ιt is very important to know that static variables are not Thread-Safe. This means if 2 or more threads modify the same static variable
, unexpected and multiple malfunctions can be caused (ex. dead locks etc) .
Create a class with name StaticVariableExample2
and paste the following code :
StaticVariableExample2.java
package com.javacodegeeks; /** * @author Petros Koulianos * */ public class StaticVariableExample2 { static int counter = 10; public static void main(String[] args) { //launch first thread with step= -1 newThread("first thread",500,1); //launch second thread with step= -3 newThread("second thread",250,3); } public static void newThread(String name , int sleep , int step) { Thread th = new Thread() { @Override public void run() { while(counter != 0) { System.out.println("**"+name+"** counter:"+counter); counter = counter - step; try { //sleep the thread to slow down the execution Thread.sleep(sleep); } catch (InterruptedException e) { e.printStackTrace(); } } } }; th.start(); } }
Example Output
**second thread** counter:10 **first thread** counter:10 **second thread** counter:6 **second thread** counter:3 **first thread** counter:3 **second thread** counter:-1 **first thread** counter:-4 **second thread** counter:-4 **second thread** counter:-8 **first thread** counter:-11 **second thread** counter:-11 **second thread** counter:-15 **first thread** counter:-18 **second thread** counter:-19 **second thread** counter:-22 **first thread** counter:-25 **second thread** counter:-26 **second thread** counter:-29 **first thread** counter:-32 **second thread** counter:-33 ............................
The above example runs 2 separate threads that modify the same static variable
differently and the result is to fall both threads into a deadlock.
7. Download the Source Code
This was a Static variable
Java example.
You can download the full source code of this example here: Static Variable Java Example
Last updated on Sept. 10, 2019