Thursday, October 28, 2021

static keyword in java

static is a non-access modifier in Java which is applicable for the following:

  1. blocks
  2. variables
  3. methods
  4. nested classes

To create a static member (block,variable,method,nested class), precede its declaration with the keyword static.

When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object.

If computations are needed in order to initialize static variables, a static block is declared that gets executed exactly once, when the class is first loaded.

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

When a method is declared with static keyword,vit is known as static method. The most common example of a static method is main( ) method. Any static method can be accessed before any objects of its class are created, and without reference to any object. Methods declared as static have several restrictions:


  • They can only directly call other static methods.
  • They can only directly access static data.
  • They cannot refer to this or super in any way.

A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name.


  • It can access static data members of the outer class, including private.
  • The static nested class cannot access non-static (instance) data members or methods

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top