Static Keyword in Java
Static Variable
In this article we will be discussing about Static Variable in Java.
The static keyword in Java is mainly used for memory management. We can use static keyword with variables, methods, blocks and nested classes. The static keyword belongs to a class rather than an instance of the class.
Static Variable:
The static keyword in Java is used to share the same variable or method of a given class. The static keyword belongs to a class rather than an instance of the class. The static keyword is used for a constant variable or method that is the same for every instance of a class.
The Static Keyword can be of following:
- Blocks
- Classes
- Variables
- Methods
Java Static Block:
If you want to use a block of statements or variable throughout the program, You can just initialize a block of variables and block of statements in a program once with using static keyword.
- Is used to initialize the static data member.
- It is executed before the main method at the time of classloading.
import java.util.*; public class Main{ //static variable static{System.out.println("Prep");} // static Function public static void main(String args[]){ System.out.println("Hello Prepster"); } }
Output: Prep Hello Prepster
Java Static Classes:
You can initialize a static class only in the nested classes, where the parent class can’t be static but the nested classes can be static. you can use these nested classes throughout the program by initializing it just once.
Example:
import java.io.*; public class Main{ // Static Variable private static String str = "PrepInsta"; // Defining the static class static class MyClass{ // display function public void disp(){ System.out.println(str); } } public static void main(String args[]){ // creating the object of Myclass MyClass temp = new MyClass(); // calling the disp function using the class object temp.disp(); } }
Output: PrepInsta
Java static variable:
You can declare any static variable using keyword “static”.
- Static Variable needs to be initialized only once at the starting of the Program.
- A static variable can be used to refer to a property common to all objects (which is not unique to each object), such as the company name of employees, student names, etc.
Syntax:
static int count = 0;
Java Static Methods:
One of the common example of static method is main() method which is defined statically. when a method is declared with the static keyword, it is known as the static method.
- A static method belongs to the class rather than the object of a class.
- A static method can be invoked without the need for creating an instance of a class.
- A static method can access static data member and can change the value of it.
Example:
import java.util.*; public class Main{ // Initializing the Static Variable static int a = 10; int b = 20; // defining static function static void m1(){ a = 20; System.out.println("The Value of a From m1 Function is " + a); } // defining a non-static function void m2(){ System.out.println("From Function m2"); } public static void main(String[] args){ // Clling the Static Variable in main function System.out.println("The Value of Static Variable a is " + a); // Calling the Static function in main function m1(); } }
Output: The Value of Static Variable a is 10 The Value of a From m1 Function is 20
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment