Programs

What is Static Keyword in Java? Various Applications Explained [2022]

Static is a reserved keyword in Java. This blog will explain how we can apply a static keyword in Java to various programming aspects, including variables, blocks, methods, and nested classes.

What is a Static Keyword in Java Programming Language?

In Java, when a static member indicates that it belongs to a type itself instead of being an instance of that type. It means that only one instance of a static member is created and shared across all class instances.

The primary method of the class is generally static. The static keyword in Java is used for a constant variable or a method that is the same for all class instances. The keyword is primarily used for memory management. Learn more about Java architecture and components.

How do you create Static Members?

Static members can be created by preceding their declarations with the keyword static. Any member declared as static can be accessed before any objects of its class are created without referencing any object.

The static keyword in Java is a non-access modifier. It can be used for –

  • Variables
  • Methods
  • Blocks
  • Nested Classes

Static Variable

Static variables are primarily global variables. When you define a variable as static, a single copy of the variable is created and shared among all objects at the class level. Static variables are created at the class level, and all the instances of the class share the same static variable.

Using Static Variables

  • When the value of the variables needs to be shared across all objects.
  • When the value of the variables is independent of the objects.

Things to Remember

  • Static variables can be declared only at the class level.
  • To access static variables, you do not need to initialize objects.
  • Static variables don’t need any object reference. They can be directly accessed using the class name.
  • Though static variables can be accessed using an object reference, it should be avoided to prevent confusion between a class variable or an instance variable. Always refer to them by using the class name.

Read: Pattern Programs in Java

Static Method

When a method is declared with the keyword static, it’s known as a static method. Static methods have the following restrictions –

  • Static methods can call other static methods only.
  • Static methods can directly access static data.

If you want to share specific code across all class instances, you need to write the code in a static method.

Static methods are also used to create helper or utility classes so that one doesn’t need to create a new object of these classes for obtaining them.

Using Static Methods

  • To manipulate or access other static methods and static variables that don’t depend upon objects.
  • They are widely used in helper and utility classes.

Things to Remember

  • Static methods can’t use ‘super’ or ‘this’ keywords.
  • Abstract methods can’t be static.
  • Static methods in Java are resolved at compile time and hence, can’t be overridden.
  • Static methods can’t access instance methods or instance variables directly. They need some object reference for doing this.

Why is the Main Method in Java Static?

As static methods don’t need an object for invoking Java, the main method is static. Had it not been static, JVM would need to have created an object before calling the main(), leading to extra memory allocation.

Static Block

Static blocks make it easy to initialize variables with predefined values. The following code demonstrates the usage of a static block that contains a variable declaration that is executed exactly once when the class is loaded.

Using Static Block

  • When static variables initialization needs some additional logic except assignment
  • When static variables initialization is error-prone and needs exception handling

Things to Remember

  • Static variables and static blocks are resolved and executed in the same order as they appear in the class.
  • Multiple static blocks can be present in a class.

Must Read: Java Project Ideas & Topics

Static Class

Only a nested class can be made static. A nested static class doesn’t need a reference for the outer class.

Using Static Nested Class

  • Increases encapsulation
  • Makes the code more manageable and increases readability
  • If the nested class doesn’t need any access to the instance members of its enclosing class, it’s a good idea to declare it as static. That way, it won’t be coupled to the outer class and won’t need any stack or heap memory.

Things to Remember

  • Static nested classes can’t access any instance members of the enclosing classes.
  • They can access all static members of the enclosing class.
  • The top-level class can’t be static.

Enroll in Software Engineering Courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Conclusion

This blog explained the static keyword in Java and its use in variables, methods, blocks, and nested classes. 

If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

How is the static keyword different from the final keyword?

A static variable or method can be accessed directly in the program without using an instance of that class. A final variable in Java can only be initialised once, and its value remains the same throughout the program, whereas a static variable can be initialised again. A final variable cannot be inherited by a variable or a subclass method. Also, classes cannot override final variables. On the other hand, static methods can call other static methods but can only access the static members of the class. A static class cannot have any objects; it will only consist of static variables and methods.

Explain the relevance of every keyword in public static void mainString args?

Firstly, we use public, an access modifier that specifies that any variable or method can access the main() method. Then, we use the static keyword, which identifies the main() method as class-based. The main() can be accessed without instantiating the class. The compiler will throw an error if you do not declare the main() method static. main() is called by the JVM (Java Virtual Machine) before all the objects are made. The return type of the main() method in Java is void which means it doesn’t return a value, and args refer to the arguments passed to the main() method while we run the code.

How are interfaces different from abstract classes?

The interfaces can only consist of abstract methods, whereas abstract classes can contain abstract and non-abstract methods. Interfaces in Java can only have static and final variables, whereas abstract classes can have both non-static and non-final variables. Interfaces enable Java developers to achieve multiple inheritance which is not possible through the conventional way in Java. Abstract classes are not used to achieve inheritance. By default, all the variables that are data members of an interface are of public visibility, whereas abstract classes can declare them as protected or private. Implementation of a interface using an abstract class is easily possible but not vice-versa.

Want to share this article?

Land on Your Dream Job

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks