Static Keyword in Java Programming

Static keyword in Java is a non-access modifier that helps to modify the meaning of a variable, method, or class.

Java contains various types of keywords or modifiers. Some of them include the following.

  • Final
  • Static
  • Abstract
  • Interface
  • We use the ‘static’ keyword to make memory location constant.
  • However, there is the possibility to change the value held in the memory.
  • Even without creating an object, it is possible to access static members of a class.
  • Static members are called class members.
An introduction to STATIC KEYWORD IN JAVA PROGRAMMING LANGUAGE

Static keywords are known either as a keyword or modifier.

Some Key Points of Static Keyword in Java:

  • The Static keyword is only used for the Data Members and Member functions.
Use of static keyword with Data members and Member Functions
Use case of static keyword with Data Members and Member Functions
  • All Static Members are first to execute.
  • However, do not use static keywords along with Classes and Constructors
  • Consequently, there are no STATIC classes in Java
Static Keyword shows Error with Class or Constructor
Static Keyword in Java shows errors with classes or constructors.
  • If there is a body for the static keyword, it is known as Static Block. 

STATIC BLOCK – has the highest PRECEDENCE among all the declarations of the class. It means it executes first, even before the static keyword.

  • NON-STATIC members CANNOT be referenced from a STATIC context
Reference of non-static members

  • All STATIC members CAN be referenced from NON-STATIC context
Static Members reference from Non Static Context

  • All NON-STATIC members participate in every instance of the class
  • In order to ACCESS NON-STATIC members from a STATIC CONTEXT – we require an OBJECT
How to access non static members from static context

  • Superclass ‘static’ method CAN be Overridden as ‘static’ ONLY
How to override super class static

  • It is not possible to declare Abstract methods as ‘static’
Abstract methods declaration

  • It is possible to access all static members outside the class using its class name.
  • You can import packages statically

Discover more from BerylSoft

Subscribe now to keep reading and get access to the full archive.

Continue reading