WHAT is STATIC VARIABLE in JAVA?

Static variable in Java is variable which belongs to the class and initialized only once at the start of the execution.

  • Static variable is a variable which belongs to the class and not to object(instance).
  • A static variable can be accessed directly by the name of the class and doesn’t need any object to access it.
  • A single copy to be shared by all instances of the class i.e. all the classes can share a static variable throughout the program.
  • Static variables can be initialized only once, at the beginning of the execution of the program. We have to initialise Static variable first, prior to the initialization of any instance variables.
  • Static variables are

Syntax: <class-name>.<variable-name>

Static variable Syntax

static keyword followed by data type, followed by variable name. 

static data_type variable_name;

static int a;

You can learn this in detail in our institute which provides advance Java training in Delhi 

WHAT is STATIC METHOD in JAVA?

This is a method in Java which belongs to the class and not to the object. A static method can access only static data.

  • This is a method which belongs to the class and not to the object(instance)
  • Static method can call only other static methods and cannot call a non-static method from it.
  • A static method can access only static data. It cannot access non-static data (instance variables)
  • A static method can be accessed directly by the class name and doesn’t need any object
  • A static method cannot refer to “this” or “super” keywords in anyway

Syntax :<class-name>.<method-name>

Let’s learn the practical aspect of Static Keyword

Example: How to call static variables & methods

 Copy the following code into an editor and Save, Compile and run the code.

STATIC VARIABLE INITIALIZATION

  1. Static variables are automatically initialized when class is loaded.
  2. Static variables are initialized before any static method of the class executes.
  3. Static variables are initialized before any object of that class is created.

Default values for static and non-static variables are same.

primitive integers (long, short etc): 0

primitive floating points (float, double): 0.0

boolean: false

object references: null

STATIC VARIABLE EXAMPLE in JAVA

OUTPUT

Obj 1:  count is =2

Obj 2:  count is =2

STATIC FINAL VARIABLES

The static final variables are constants. Let’s see the code below:

public class MyFirstClass

{

public static final int MY_FIRSTVAR=27;

}

1) The variable MY_FIRSTVAR is public which means it can be used by any class in the program. It is a static variable so we won’t need any object of class in order to access it. It is final so the value of this variable can never be changed either in the current class or in any other class.

2) The above code will get executed as soon as the class MyFirstClass is loaded, even before static method is called and before any static variable can be accessed.

Java Static Keyword – Let’s explore

Java is a general purpose, object-oriented programming language. Latest version is Java 9, released on September 21,2017 has various functionalities. We are going to discuss Static Keyword today, Codec Networks provides best Java Training & Certification in Delhi.

About

Codec Networks provides IT Trainings from EC Council CEH ECSA, LPT, CHFI, Network Security, Penetration Testing, ISACA, ISC2, PECB ISO 27001LA LI, Cisco Networking CCNA CCNP, Linux Administration RHCE, Prog Languages JAVA, Advanced Java, android development. We also offer B2B Industry Solutions and Services in IT | Information|Cyber Security in Delhi NCR India.

View all posts by