A static keyword in Java is utilized predominantly for memory management. Variables, procedures, blocks, and nested groups may be used to use it. It is a keyword in Java that is being utilized to connect or share a given class with the same value of variable or process. Effectively, static is used for each instance of a class for a constant variable or a process that is the same. A Java class’s primary procedure is generally called java static.
You need to invoke its definition with the static keyword in java to construct a static participant (block, parameter, process, nested class). If an element of a class is labeled static, this can be accessed even before class objects are constructed without any kind of reference objects.
Table of Contents
What is the usage of static keyword in Java?
Static in Java can be utilized in any way from the below:
- Methodologies
- Variable
- The block
- The class that is nested
Let’s have a look at the basic use of static keyword in java:
public class Main {
public static void main( String[] args ) {
// Let’s access the methodologies of Math class
System.out.println("An absolute value of -99 = " + Math.abs(-99));
System.out.println("Desired value of pi is = " + Math.PI);
System.out.println("Desired value of E is = " + Math.E);
System.out.println("6^2 is = " + Math.pow(6,2));
}
}
Output:
// An absolute value of -99 = 99
// Desired value of pi is = 3.141592653589793
// Desired value of E is = 2.718281828459045
// 6^2 = 36.0
Static variables utilized in Java
The static keyword in java is being used to construct variables independently of just about any cases generated for the class that may occur. There is only one replica of the static variable, irrespective of the class’s number of instances.
Java static variables are often referred to as variables of a class. It’s not possible to declare local quantities static.
The below-represented illustration utilizes the static variable in java language:
class demo_Sj
{
// Let’s declare static parameter or variable
static int a_sj= sj();
// Let’s declare the java static block
static{
System.out.println(“This methodology is inside static block!”);
// Let’s declare the methodology of static
static int a_sj() {
System.out.println(“This is from the side of a_sj”);
// Let’s declare static method from the main block
public static void main(String args[])
{
System.out.println(“The value of a: ”+a);
System.out.println(“This is from main static block!”);
}}
Output:
// This is from the side of a_sj
// This methodology is inside static block!
// The value of a: 15
// This is from main static block!
Static methodologies utilized in Java
When a methodology with a static keyword is defined, it is recognized as a static method. The main() technique is the most appropriate example of a static method. It is possible to access any static member even before members of its class are constructed and without connection to any object. There are many constraints on procedures declared static:
- They are only able to call other static methods specifically.
- The static data can be accessed by them.
- They can not term to keyword super or keyword this in any possible way.
Let’s have a grasp on the illustration of static methodologies that are utilized in Java:
// Let's declare or define a class that calculates the square of some number that is an integer by type
class calc_sj{
static int squ_sj (int g){
return g*g;
}
// Let’s declare the main methodology
public static void main(String args[])
{
int sj_output= calc_sj.squ_sj(12);
System.out.println(“The desired output by calculating the square of a given integer number is: ” +sj_output);
}}
Output:
// The desired output by calculating the square of a given integer number is: 144
Static blocks utilized in Java
Whenever the class is loaded into memory, the static block will only be executed once. Whenever the object of the class is asked in code or the static members are asked in the script, the class is enabled.
A class may have several static blocks and then each static block is implemented in the very same chain in which they’ve developed in a code.
The illustration below represents a block that is static in Java:
class sj_main {
// This below defined are variable that are static
static int sj_a = 76;
static int sj_b;
static int sj_maxm;
// This below defined are the blocks that are static
static {
System.out.println("Welcome to 1st static block of this code");
Sj_b = sj_a * 2;
}
static {
System.out.println("Welcome to 2nd static block of this code");
Sj_maxm = 56;
}
// This below declared is static methodology
static void sj_display_demo() {
System.out.println("a in this code is = " + sj_a);
System.out.println("b in this code is = " + sj_b);
System.out.println("Maximum in this code is = " + sj_maxm);
}
// Let’s declare the main methodology
public static void main(String args[]) {
// Let’s call the static methodology
sj_display_demo();
}
}
Output:
// Welcome to 1st static block of this code
// welcome to 2nd static block of this code
// a in this code is = 76
// b in this code is = 152
// Maximum in this code is = 56