Java Variable types in Hindi – Static, Instance and Local

1
982
Java Variables Types, Core java Tutorials, Java Variable Types : Static, Instance and Local, Core java Tutorials, Programming practicals, core Java GuideProgramming practicals, core Java Guide
Java Variable Types : Static, Instance and Local, Core java Tutorials, Programming practicals, core Java Guide

Java Variable Types in Hindi : Static, Instance and Local

Definition of Java Variable:- Java Variable is address/name of memory location. Java Variable is name of reserved area allocated in Memory. Based on the type of value java variables are divided into two types.

  • Primitive variable

  • Reference variable

Primitive  variable :- it can be used to represented by primitive value.

int  i = 20;

Reference variable :-  it can be used to represented by objects.

                Go4Vidhya  go = new  Go4Vidhya();

based on the purpose and position we are divided all java variable types into 3 categories.

  • Instance variable

  • Static variable

  • Local variable

Instance Java variable

Instance variable also known as “object level variables” or “attribute”.

If  the Value of java variable is varied from object to object such type of variables are called Instance variable.

For every object a separate copy of instance variable will be created.

The scope of the instance variable is exactly the same as the scope of the objects. Because instance variable will be created at the time of objects creation & destroy at the time of  objects destruction.

Instance variables will be stored as a part of object.

Instance variable should be declare with in the class directly but outside of any method or block or constructor.

Instance variable cannot be access from static area directly we can access by using object reference.

Note : But from instance area we can access instance member directly.

For more clarity I have written one program also.

For more information and more clarity take a simple example here.

Java Variable Types in Hindi, Core java Tutorials, Java Variable Types : Static, Instance and Local, Core java Tutorials, Programming practicals, core Java GuideProgramming practicals, core Java Guide
“Java Variable Types : Static, Instance and Local”

This program is not working as per our requirement.

Example:-

package Core;

public class InstanceVaribleDemo {

int i = 10; //instance variable. Require static variable for below access.

public static void main(String[] args) {

System.out.println(i);  //CE(error) we cant access this variable.

}

public void javguide() {

System.out.println(“value i is instance level” +i);

}

}

Below program is working properly.

Example:-

package Core;

public class InstanceVaribleDemo {

int i = 10;

public static void main(String[] args) {

InstanceVaribleDemo javGuide = new InstanceVaribleDemo();//create object

System.out.println(javGuide.i); // for accessing instance variable

}

public void javguide() {

System.out.println(“value i is instance level” +i);

}

}

Output : 10

For the instance variable it is not required to perform initialization explicitly ,  JVM provide default value.

We have written simple program here for more clarity for java variable

Example:-

package Core;

public class InstanceVaribleDemo {

int i;

String s;

boolean b;

float f;

public static void main(String[] args) {

InstanceVaribleDemo javaguide = new InstanceVaribleDemo();

System.out.println(javaguide.i);

System.out.println(javaguide.s);

System.out.println(javaguide.b);

System.out.println(javaguide.f);

}

}

Output:

0

null

false

0.0

 

Static Java variable

If  the value of variable is not varied from object to object then it is never recommended to declare that  variable at object level. We have to declare such type of variables at class level by using static modifier.

If the cost of the instance variable for every object a separate copy will be created. But in the case of a static variable single copy will be created at class level & the copy will be shared by all objects of that class.

Static variable will be created at the time of class Loading & destroy at the time of class unloading. Hence the scope of static variable is exactly same as the scope of the class.

Execution process you should have to know.

Java test

  • Start jvm
  • Create main thread
  • Locate test.class
  • Load test.class
  • Execute main() method of test.class
  • Unload test.class
  • Destroy main thread
  • Shutdown jvm

Static variable should be declare with in class directly (but outside of any method or block or constructor) with static modifier.

Static variable can be accessed either by using class name or by using object reference, but recommended to use class name.

With in the same class even it’s not required to use class name also we can access directly.

Example:-

class  Test

{

Static int x = 10;

public static void main(String args[])

{

System.out.println(Test.x);

System.out.println(x);

Test t = new Test();

System.out.println(t.x);

}

}

Output: 10

 

Static variable will be stored in method area static variable also known as “class-level variable”  or “fields”.

Example:-

class Test

{

int x = 10;

static int y = 20;

public static void main(String args[])

{

Test T1 = new Test();

T1.x=888;

T1.y= 999;

Test T2 = new Test();

System.out.println(T1.x+”….”+T2.y);

}

}

If we  performing any change for instance variable these changes will not be reflected for the remaining objects. Because, for every object a separate copy of instance variable will be their.

But, if we are performing any change to the static variable , these changes will be reflected for all objects because we are maintaining a single copy.

Static variables are created at the time of class loading. (at the beginning of the program) hence, we can access from both instance & static areas directly.

Example:-

class Test

{

static int x=10;

public static void main(String[] args)

{

System.out.println(x);

}

public void m1()

{

System.out.println(x);

}

}

For the static variables it is not required to perform initialization. Exactly , Compulsory JVM will provide default values.

Example:-

class Test

{

static int x;

public static void main(String[] args)

{

System.out.println(x);

}

}

For the local variables JVM wait provide any default values. Compulsory we should perform initialization exactly before using that variable.

Example:-

class Test

{

public static void main(String[] args)

{

int x;

System.out.println(“Hello”);

}

}

Output: Hello

Example:-

class Test

{

public static void main(String[] args)

{

int x;

System.out.println(x);

}

}

Output: Compile time error(C.E) – x might not have been initialized

Example:-

class Test

{

public static void main(String[] args)

{

int x;

if(args.length>0)

{

X=10;

}

System.out.println(x);

}

}

Output will get compile time error- variable x might not have been initialized.

Local Java Variable

To meet temporary requirement of the programmer sometimes we have to create variables inside method or block or constructor.

Such type of variables are called Local Variables.

Local variables also known as static variables or Automatic variables or temporary variables.

Local variables will be stored inside a stack.

The local variables will be created while executing the block in which we declared it & destroyed once the block completed. Hence, the scope of local variable is exactly same as the block in which we declared it.

Example:-

class Test

{

public static void main(String[] args)

{

int i=0;

for(int j=0; j<3; j++)

{

i=i+j;

}

System.out.println(i+”…….”+j);

}

}

Example:-

class Test

{

public static void main(String[] args)

{

int x;

if(args.length>0)

{

x=10;

}

else

{

x=20;

}

System.out.println(x);

}

}

Output: Java Test

20

Java Test x y

10

Note: It is not recommended to perform initialization & Local variables inside logical blocks because these is no guarantee execution of these blocks at runtime.

It is highly recommended to perform initialization for the local variable type at the time of declaration, at least with default values.

the any applicable modifier for the local variables is “final”.

if we are using any other modifier we will get compile time error.

Example:-

class test

{

public static void main(String[] args)

{

private int x=10;

private int x=10;

protected int x=10;

static int x=10;

final int x=10;

}

}

Top 5 core java tutorials

What is the Difference between length and length()?

https://go4vidhya.com/what-is-the-difference-between-length-and-length/

Java Arrays

Java Arrays in Hindi- 2D,3D Arrays

Java Anonymous Array

https://go4vidhya.com/java-anonymous-array-in-hindi/

Java Literals

Java Literals in Hindi- What are Java Literals?, Why We use Java Literals

Java Datatypes

Java Data types in Hindi

Java Identifiers

Java Identifiers in Hindi- What are Java Identifiers and Keywords

Features of java in Hindi

Features of Java in Hindi- What are the Features of Java in Hindi

History of Java in Hindi

https://go4vidhya.com/history-of-java-in-hindi/

Introduction about Core java and Go4Vidhya in Hindi

https://go4vidhya.com/introduction-about-core-java-and-go4vidhya-in-hindi/

thanks for visit- by Go4Vidhya

hope you like

Java Variable types in Hindi

LEAVE A REPLY

Please enter your comment!
Please enter your name here