articles

Home / DeveloperSection / Articles / Use of Static and Non-static modifiers with method and variable

Use of Static and Non-static modifiers with method and variable

Hemant Patel 1704 20-Jan-2017

Static variable:- Those variable of the class that are declared by using static modifier are known as static variables.

Memory allocation of these variable has been done at class loading time(AS we know JVM loads every class in the memory in order to use variables and methods of that class).

Since a class is loaded only once in the memory.So memory allocation of the static variables has also been done only once.

A static variable has only one copy in the memory.So memory allocation of the static variables has also been done only once. These variables is also known as class variables.

Object reference is not required to access these variables,but can be given.

      class AA
{
static int x;
public static void main(String [] args)
{
x=90;
system.out.println(x);
}
}

 Non-static variable:- Those variables of the class that are defined without using static modifier is known as non-static variables.

These variables occupies memory each times an instance (object) of the class is created. It means  memory allocation of these variables are also done without creating object. These variables are also known as instance variable or field of the object.

Every objects of the class maintains separate copy  of these variables. i.e. why these variables can not be accessed without giving the reference of the object.

        class AA
{
int x ; //non static variable
public static void main(String [] args)
{
AA obj=new AA();
obj.x=90;
System.out.println(obj.x);
}
}

Every field of the object must be referenced in order to accessed them.

 Static method:- A method which is defined by using static modifiers is known as static method.This method can be called without giving reference of the object.

             class ST
{
static void show()
{
System.out.println(“Show method of class ST executed”);
}
public static void main(string[] args)
{
show();
}
}

Parameters of the methods can be eighter primitive or non-premitive then it will accept only reference of the object.

                   class ST
{
static void show(ST t)
{
System.out.println(‘’Show method of class ST is executed”);
}
public static void main(string [] args)
{
ST obj=new ST();
show(obj)
}
}

Non-static method:- Those methods that are defined without using static modifier are known as non-static methods.

In case of non-static method fields will be referenced by the compiler. It means reference will be given by the compiler in order to reference by the field.

But at the calling time of method you will have give reference to that method. Compiler will use to that method.

                 class Calcie
{
int x,y,z;
void set(int a,int b)
{
x=a;
y=b;
}
void add()
{
z=x+y;
System.out.println(z);
}
void multi()
{
z=x*y;
System.out.println(z);
  }
public static void main(string [] args)
{
Calcie obj=new Calcie();
obj.set(10,50);
obj.add();
obj.multi();
}
}

Updated 31-Mar-2019
Exploring the online world is my passion. have experience of #content writing #SEO #Digital Marketing #On-Page #Lead Generation #Content Analyst #Marketing Analyst... I could never stop at one point, continuously wanted to acquire more and more skills. My work for any organization will be full of passion and hard work.

Leave Comment

Comments

Liked By