Static variable, Static functions & Static block

Here we will try to under Static keyword

Static keyword in java:

The static keyword is used in java mainly for memory management. We may apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class.

  • variable (also known as class variable)
  • method (also known as class method)
  • block

Static variable:

If you declare any variable as static, it is a known static variable.

The static variable can be used to refer to the common property of all objects (that is not unique for each object) e.g. company name of employees, college name of students, etc.

The static variable gets memory only once in the class area at the time of class loading.

Advantage of static variable:

It makes your program memory efficient (i.e it saves memory).

Suppose there are 500 students in my college, now all in x            stance data members will get memory each time when the object is created. All students have their unique roll no and name so instance data member is good. Here, college refers to the common property of all objects. If we make it static, this field will get memory only once

Example:

public class StaticKeyWord {

String name;
int rolno;
String group;
static String collegeName;
public static void main(String[] args) {
StaticKeyWord st1=new StaticKeyWord();
st1.init(“Ashok”, 1, “Mpc”);
StaticKeyWord st2=new StaticKeyWord();
StaticKeyWord st3=new StaticKeyWord();
st2.init(“Ankush”, 2, “Mpc”);
st3.init(“DhanShree”, 2, “Bpc”);
StaticKeyWord.collegeName();
st1.display();
st2.display();
st3.display();
}

public void init(String nametmp,int rolnotmp,String grouptmp)
{
name=nametmp;
rolno=rolnotmp;
group=grouptmp;
}
public void display()
{
System.out.println(“Student name is ” +name);
System.out.println(“Student Id is ” +rolno);
System.out.println(“Student group is ” +group);
System.out.println(“CollegeName is ” +collegeName);

}

In the above example, we have declared one static variable called “collegeName”.  Here we are creating 3 objects called st1,st2, and st3. But for all of these 3 objects, only one memory location created for a variable called “collegeName” and all the objects are going to share same memory location.

Static method:

If you apply a static keyword with any method, it is known as the static method

A static method belongs to the class rather than the object of a class.

A static method can be invoked without the need for creating an instance of a class. Static methods are called by class name. ex classname.methodName();

The static method can access only static data members and can change the value.

Restrictions for static method:

  • The static method cannot use non-static data members or call a non-static method directly.
  • this and super keywords cannot be used in a static context.

Let’s try to understand why we can not use “this” keyword with static:

“this” keyword always represents at the object level and static represents at the class level. Since static is at class level and “this” keyword is at object level we cannot use this combination.

Example:

package javaexample;
public class StaticKeyWord {

String name;
int rolno;
String group;
static String collegeName;

public static void main(String[] args) {
// TODO Auto-generated method stub
StaticKeyWord st1=new StaticKeyWord();
st1.init(“Ashok”, 1, “Mpc”);
StaticKeyWord st2=new StaticKeyWord();
StaticKeyWord st3=new StaticKeyWord();
st2.init(“Ankush”, 2, “Mpc”);
st3.init(“DhanShree”, 2, “Bpc”);
StaticKeyWord.collegeName();
st1.display();
st2.display();
st3.display();
}
public static void collegeName()
{
collegeName=”Abc”;
}
public void init(String name,int rolno,String group)
{
this.name=name;
this.rolno=rolno;
this.group=group;
}
public void display()
{
System.out.println(“Student name is ” +name);
System.out.println(“Student Id is ” +rolno);
System.out.println(“Student group is ” +group);
System.out.println(“CollegeName is ” +collegeName);

}
}

Here we defined one static method called “collegeName” and assigned value to a static variable.

Static block:

Is used to initialize the static data member.

It is executed before the main method at the time of class loading

Example:

public class StaticBlock {

public static void main(String[] args) {
System.out.println(“Main Method executed”);
}
static {
System.out.println(“Static blcok executed”);
}

}

Here we have one static block and it will execute first, before main method itself.

Why the main method is static?

Because the object is not required to call static method if it were non-static method, jvm create an object first then call the main() method that will lead to the problem of extra memory allocation

Leave a Comment

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

18 Years Experienced Trainer