Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
In Java, keywords are the reserved words that cannot be used as identifiers. In total there are 57 keywords in Java. One among them is “Static“. In this article, I will give you a brief insight into how static keyword in Java is applicable for various aspects of programming.
Below topics are covered in this article:
In Java, static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes. It is a keyword which is used to share the same variable or method of a given class. Basically, static is used for a constant variable or a method that is same for every instance of a class. The main method of a class is generally labeled static.
In order to create a static member (block, variable, method, nested class), you need to precede its declaration with the keyword static. When a member of the class is declared as static, it can be accessed before the objects of its class are created, and without any object reference.
In Java programming language, static keyword is a non-access modifier and can be used for the following:
Let’s get into the details of each of these methods with the help of an example.
Let’s first understand how static block is used in the Java programming language.
If you need to do the computation in order to initialize your static variables, you can declare a static block that gets executed exactly once, when the class is first loaded. Take a look at the below Java program to understand the usage of Static Block.
// Java program to demonstrate the use of static blocks import java.util.*; public class BlockExample{ // static variable static int j = 10; static int n; // static block static { System.out.println("Static block initialized."); n = j * 8; } public static void main(String[] args) { System.out.println("Inside main method"); System.out.println("Value of j : "+j); System.out.println("Value of n : "+n); } }
When you execute the above program, static block gets initialized and displays the values of the initialized variables.
Static block initialized Inside main method Value of j:10 Value of n : 80
Now that you know how static block works, let’s move further and see what are static variables and how it is helpful.
When you declare a variable as static, then a single copy of the variable is created and divided among all objects at the class level. Static variables are, essentially, global variables. Basically, all the instances of the class share the same static variable. Static variables can be created at class-level only.
Now let’s understand this with the help of an example.
// Java program demonstrate execution of static blocks and variables import java.util.*; public class VariableExample { // static variable static int j = n(); // static block static { System.out.println("Inside the static block"); } // static method static int n() { System.out.println("from n "); return 20; } // static method(main !!) public static void main(String[] args) { System.out.println("Value of j : "+j); System.out.println("Inside main method"); } }
When you execute the above program, it will execute static block and the variable in order as defined in the above program.
from n Inside the static block Value of j: 20 Inside main method
Having understood this, let’s dive deeper into this article on Static keyword in Java and know what are Static methods and nested classes.
When a method is declared with the static keyword, it is known as a static method. The most common example of a static method is the main( ) method. Methods declared as static can have the following restrictions:
They can directly call other static methods only.
They can access static data directly.
Now let’s understand static methods with the help of an example
// java program to demonstrate restriction on static methods public class StaticMethodExample { // static variable static int j = 100; // instance variable int n = 200; // static method static void a() { a = 200; System.out.println("Print from a"); // Cannot make a static reference to the non-static field b n = 100; // compilation error // Cannot make a static reference to the // non-static method a2() from the type Test a2(); // compilation error // Cannot use super in a static context System.out.println(super.j); // compiler error } // instance method void a2() { System.out.println("Inside a2"); } public static void main(String[] args) { // main method } }
In the above examples, you can see how the restrictions are imposed on the static methods and also how you are allowed to use super keyword in the static context. That was all about Static Methods. Now let’s see what are nested classes.
A class can be made static only if it is a nested class. Nested static class doesn’t need a reference of Outer class. In this case, a static class cannot access non-static members of the Outer class. Let’s take an example to understand how it works
public class NestedExample{ private static String str= "Edureka" //Static class static class MyNestedClass{ //non-static method public void disp(){ System.out.println(str); } } public static void main(String args[]){ NestedExample.MyNestedClass obj = new NestedExample.MyNestedClass(); obj.disp(); }
When you execute the above code, your output looks like:
Edureka
That’s was all about various applications of Static Keyword in Java. With this, we come to the end of this article. I hope you found it informative. If you wish to learn more, you can check out our other Java Blogs as well. If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.
Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this “ Static Keyword in Java” article and we will get back to you as soon as possible or you can also join Java Training in Makassar.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 28th September,2024 28th September SAT&SUN (Weekend Batch) | View Details |
edureka.co