The Static Keyword is a non-access modifier in Java mainly used for memory management. This static keyword is mainly applicable for the following:
- Variables
- Methods
- Blocks
- Nested Class
Static Variable is a variable that is declared as static means a single copy of the variable is created and shared among all objects at the class level. Static method is a method belonging to the class rather than the object of a class as well as a static method can be invoked without the need for creating an instance of a class.
Note:
- The static variable gets memory only once in the class
- A static method can access static data members and can change the value of it.
Now here we will be discussing out both the cases with a simple demonstration via clean java programs as follows:
- Case 1: Accessibility of a static method/s by static variable/s
- Case 2: Accessibility of a static variable by static method
Case 1: Accessibility of the static methods by static variables
Declaring the variable as static and after that, we called that variable in a static method so that we get that variable as an output.
Example:
Java
// Java Program to demonstrate Accessibility of a static // method/s by static variable/s // Importing input output classes import java.io.*; // Main class public class GFG { // Declaring static variable static int i = 10 ; // Main method public static void main(String[] args) { // Print and display the static variable System.out.println( "Static Variable = " + i); } } |
Static Variable = 10
Time Complexity: O(1)
Auxiliary Space: O(1)
Case 2: Accessibility of a static variable by static method
Declaring the variable, array, and method as static where the static variable and the static array are accessed by the static method. The static method is called without creating an instance of the class.
Example:
Java
// Java Program to demonstrate Accessibility of an static // variable by static method // Importing input output classes import java.io.*; // Main class class GFG { // Declaring and initializing variables // Making variables static static int i = 10 ; static int j = 20 ; // Declaring the static temporary array static int temp[] = { 2 , 6 , 3 , 0 , 1 , 7 }; // Method 1 // Multiplication of array elements public static void multiply( int n) { for ( int k = 0 ; k < n; k++) { // Multiplying each element of array with i=10 temp[k] = temp[k] * i; } } // Method 2 // To print an array public static void print_Array( int n) { // Display message System.out.print( "\nArray = " ); // Iteration using for loop to print complete array for ( int m = 0 ; m < n; m++) { // Printing array element System.out.print(temp[m] + " " ); } } // Method 3 // Main driver method public static void main(String[] args) { // TODO Auto-generated method stub System.out.print( "Static variable : " + i); // Length of static array temp[] int n = temp.length; // Calling the static multiply method multiply(n); // Calling the static Print_Array method print_Array(n); } } |
Static variable : 10 Array = 20 60 30 0 10 70
Time Complexity: O(n)
Auxiliary Space: O(1)