Static Member and Scope in Java
Static Keyword in Java
Static Member and Scope in Java is used to increase the memory efficiency by utilizing less space. As a static variable can be accessed by any member of a class without initializing again.
The Static could be:
- Variable
- Method
- Block
Static Member and Scope in Java
Static Variable in Java
- A static variable’s value is set when it is defined, and we do not need to make an instance of it in order to utilise it.
- Memory is only allocated once because the variable is only created once.
- Static variables in Java are stored in the heap memory
Add Your Heading Text Here
Static Variable
class emp { String Employee_id;//This is an Instance Variable String Name;//This is an Instance Variable static String Company = "PrepInsta"; emp(String id, String name) { Employee_id = id; Name = name; } void display() { System.out.println(Employee_id + " " + Name + " " + Company); } } public class Main { public static void main(String args[]) { emp s1 = new emp("P01", "XYZ"); emp s2 = new emp("P02", "ABC"); s1.display(); s2.display(); } }
Output
P01 XYZ PrepInsta
P02 ABC PrepInsta
P02 ABC PrepInsta
Static Methods in Java
- Only static variables can be accessed and modified by static methods.
- It is possible to call or use static methods without first creating a class instance.
Static Method
Run
class emp { String Employee_id; String Name; static String Company = "PrepInsta";//Static Variable</pre> emp(String id, String name) { Employee_id = id; Name = name; } void display() { System.out.println(Employee_id + " " + Name + " " + Company); } static void Change() {//Static Method Company = "PrepInsta Prime"; } } public class Main { public static void main(String args[]) { emp e1 = new emp("P01", "XYZ"); emp e2 = new emp("P02", "ABC"); System.out.println("Before\n"); e1.display(); e2.display(); emp.Change(); System.out.println("\nAfter\n"); e1.display(); e2.display(); } }
Output
Before
P01 XYZ PrepInsta
P02 ABC PrepInsta
After
P01 XYZ PrepInsta Prime
P02 ABC PrepInsta Prime
P01 XYZ PrepInsta
P02 ABC PrepInsta
After
P01 XYZ PrepInsta Prime
P02 ABC PrepInsta Prime
Static Block in Java
When a Java class is first loaded into the JVM, a block of statements known as the static block is executed. Similar to how constructors aid in initialising instance members, a static block aids in initialising the members of static data.
Static Block
Run
class Main{ static{ System.out.println("Static Block Executed by default"); } public static void main(String args[]){ System.out.println("PrepInsta"); } }
Output
Static Block Executed by default
PrepInsta
PrepInsta
Scope of Static Variable
- Static variables have a single copy that can be shared by all of the class’s objects and are allocated memory only once. Static variables improve the program’s memory efficiency as a result.
- Unlike static variables, which only have one copy made for a class, instance variables create independent copies for each object.
- Hence, When a class is loaded, static variables are created, and when a class is unloaded, they are destroyed. Therefore, the scope of the static variable and the class are same.
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