[Go to site: main page, start]

0% found this document useful (0 votes)
7 views6 pages

Understanding Java Variable Types

The document outlines the three types of variables in programming: instance variables, static variables, and local variables. Instance variables are unique to each object and created during object instantiation, while static variables are shared across all instances of a class. Local variables are temporary and exist only within the scope of a method or block, requiring explicit initialization before use.

Uploaded by

lsrinivas.rpa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Understanding Java Variable Types

The document outlines the three types of variables in programming: instance variables, static variables, and local variables. Instance variables are unique to each object and created during object instantiation, while static variables are shared across all instances of a class. Local variables are temporary and exist only within the scope of a method or block, requiring explicit initialization before use.

Uploaded by

lsrinivas.rpa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Based on the behaviour and position of declaration all variables are divided

into the following 3 types.

1. Instance variables

2. Static variables

3. Local variables

Instance variables:

 If the value of a variable is varied from object to object such type of variables are

called instance variables.

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

 Instance variables will be created at the time of object creation and destroyed at

the time of object destruction hence the scope of instance variables is exactly

same as scope of objects.

 Instance variables will be stored on the heap as the part of object.

 Instance variables should be declared with in the class directly but outside of any

method or block or constructor.

 Instance variables can be accessed directly from Instance area. But cannot be

accessed directly from static area.

 But by using object reference we can access instance variables from static area.

Example:

class Test

int i=10;

public static void main(String[] args)

//[Link](i);

//non-static variable i cannot be referenced from a static

context(invalid)

Test t=new Test();


[Link](t.i);//10(valid)

[Link]();

public void methodOne()

[Link](i);//10(valid)

For the instance variables it is not required to perform initialization JVM will always

provide default values.

Example:

class Test

boolean b;

public static void main(String[] args)

Test t=new Test();

[Link](t.b);//false

Instance variables also known as object level variables or attributes.

Static variables:

 If the value of a variable is not varied from object to object such type of variables

is not recommended to declare as instance variables. We have to declare such

type of variables at class level by using static modifier.

 In the case of instance variables for every object a separate copy will be created

but in the case of static variables for entire class only one copy will be created

and shared by every object of that class.

 Static variables will be crated at the time of class loading and destroyed at the

time of class unloading hence the scope of the static variable is exactly same as

the scope of the .class file.


 Static variables will be stored in method area. Static variables should be declared

with in the class directly but outside of any method or block or constructor.

 Static variables can be accessed from both instance and static areas directly.

 We can access static variables either by class name or by object reference but

usage of class name is recommended.

 But within the same class it is not required to use class name we can access

directly.

For the static variables it is not required to perform initialization explicitly, JVM will

always provide default values.

Example:

class Test

static String s;

public static void main(String[] args)

[Link](s);//null

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();

[Link](t2.x+"----"+t2.y);//10----999
}

Diagram:

Static variables also known as class level variables or fields.

Local variables:

Some times to meet temporary requirements of the programmer we can declare

variables inside a method or block or constructors such type of variables are called local

variables or automatic variables or temporary variables or stack variables.

Local variables will be stored inside stack.

The local variables will be created as part of the block execution in which it is declared

and destroyed once that block execution completes. Hence the scope of the local

variables is exactly same as scope of the block in which we declared.

Example 1:

class Test

public static void main(String[] args)

int i=0;

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

i=i+j;

}
[Link](i+”-----”+j); // invalid, this will give you error

}}

 The local variables will be stored on the stack.

 For the local variables JVM won't provide any default values compulsory we

should perform initialization explicitly before using that variable.

 It is never recommended to perform initialization for the local variables inside

logical blocks because there is no guarantee of executing that block always at

runtime.

 It is highly recommended to perform initialization for the local variables at the

time of declaration at least with default values.

Note: The only applicable modifier for 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)

public int x=10; //(invalid)

private int x=10; //(invalid)

protected int x=10; //(invalid) C.E: illegal start of

expression

static int x=10; //(invalid)

final int x=10;//(valid)

Conclusions:

1. For the static and instance variables it is not required to perform initialization

explicitly JVM will provide default values. But for the local variables JVM won't

provide any default values compulsory we should perform initialization explicitly


before using that variable.

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

entire class a single copy of static variable will be created. For every Thread a

separate copy of local variable will be created.

3. Instance and static variables can be accessed by multiple Threads simultaneously

and hence these are not Thread safe but local variables can be accessed by only

one Thread at a time and hence local variables are Thread safe.

4. If we are not declaring any modifier explicitly then it means default modifier but

this rule is applicable only for static and instance variables but not local variable.

Common questions

Powered by AI

If no modifier is declared for a variable explicitly, the variable assumes a 'default' (or package-private) access level, meaning it is accessible only within its own package. This implicit default applies to both static and instance variables but not to local variables, where specifying a modifier is generally not possible aside from 'final' .

Assuming local variables are initialized by the JVM, as instance and static variables are, can lead to runtime errors like NullPointerException or other bugs due to accessing variables that contain garbage data or undefined values. This assumption can lead to logical errors in program execution where initial setup or independent computations rely on default initialization that does not occur, making robust error handling and purposeful initialization strategies critical in variable management .

Instance variables are specific to each object, meaning each object gets its own copy, and they are created when the object is created and destroyed when the object is destroyed. They are part of the object stored on the heap. Conversely, static variables are shared across all instances of a class; only one copy exists for the entire class, stored in the method area. They are created when the class is loaded and destroyed when the class is unloaded .

For uninitialized instance or static variables, the JVM provides default values. For boolean variables, the default value is 'false'. For String variables (which are objects), the default is 'null' since they are reference types and not primitives .

Instance and static variables are not thread-safe because they can be accessed and modified by multiple threads simultaneously. This is due to their storage location and sharing characteristics; instance variables exist per object and can be shared across multiple threads accessing the same object, while static variables, being shared at the class level, can be accessed by any thread that has loaded the class. Conversely, local variables are thread-safe because they are stored in a stack specific to a thread, meaning each thread gets its own copy when executing a block, preventing concurrent access by multiple threads .

Using the class name to access static variables is preferred as it clarifies the intent that the variable belongs to the class itself rather than any particular instance. This practice enhances code readability and understanding, clearly differentiating between class-level data and instance-specific data, which is crucial when maintaining or debugging code .

Local variables differ in that they must be explicitly initialized before use, unlike instance and static variables, which the JVM can initialize with default values. Local variables have a scope limited to the block in which they are declared and are created when the block is executed and destroyed once execution of the block ends. They are stored in the stack, whereas instance variables are stored on the heap, and static variables in the method area .

Static variables are tied to the lifecycle of the class rather than its instances. They are created when the class is loaded into memory, which happens before any instance of the class is created, and they are destroyed when the class is unloaded, usually when the JVM process ends or when a class loader that loaded the class is no longer in use. This ensures their value is preserved and shared across all class instances during the class's lifecycle .

Static variables are beneficial when the value of a variable needs to be consistent across all instances of a class, such as configuration settings or counters that should track data shared by all instances. By storing such data at the class level, rather than per object, memory usage is reduced since only one copy of the variable exists, and all instances read from or write to the same memory location .

Initializing local variables within logical blocks like loops or conditionals is discouraged because there is no certainty these blocks will execute at runtime. This can lead to situations where local variables remain uninitialized, resulting in errors. Therefore, it is recommended to initialize them at the time of declaration, ensuring they always have a value .

You might also like