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

Understanding Java Variables and Types

1) Variables in Java are data containers that hold values during program execution. Every variable has a corresponding data type like int or String. 2) There are three types of variables in Java - global/class variables, instance/non-static variables, and local variables. Global variables are declared at the class level and can be accessed from any method. Instance variables are declared within a class but outside methods and require object creation to access. Local variables are declared within methods and their scope is limited to the method block. 3) Each variable type has its own rules - global variables values are shared among all class instances, instance variable values are unique per object, and local variables must be initialized before use and have

Uploaded by

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

Understanding Java Variables and Types

1) Variables in Java are data containers that hold values during program execution. Every variable has a corresponding data type like int or String. 2) There are three types of variables in Java - global/class variables, instance/non-static variables, and local variables. Global variables are declared at the class level and can be accessed from any method. Instance variables are declared within a class but outside methods and require object creation to access. Local variables are declared within methods and their scope is limited to the method block. 3) Each variable type has its own rules - global variables values are shared among all class instances, instance variable values are unique per object, and local variables must be initialized before use and have

Uploaded by

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

Variables in Java-

It is nothing but data conatiner which holde values during program excution.

Every variables always have the corresponding data type.

int rollNo=10;

types of variable-

1) global variable-

It is declare at class level or global level.

a) Static variable-

1) We can access the static variable diretly without creation of object.


As there static memory get alloted when class is loaded.

ex-
package Variables;

public class StaticVariable {

static int x=10;

public static void main(String[] args) {

[Link](x);

}
o/p-
10

2) When there is no initalization of static variable then we will get the default
value for data.

ex-
package Variables;

public class StaticVariable {

static String x;

public static void main(String[] args) {

[Link](x);

}
o/p-
null
3) Values of static variable will be shared among objects of class.

4) If static variable value change then new value will be overrite and shared among
the class members and objects.
ex-package Variables;

public class StaticVariable {

static int x=10;

public static void main(String[] args) {

[Link](x);

StaticVariable av=new StaticVariable();

[Link](av.x);

x=50;

[Link](x);

[Link](av.x);

}
o/p-
10
10
50
50

5) scope of static variable will be through out the class.


-----------------------------------------------------------
b) Non-static variable or instance variable
-
Rule-

1) to call the instance variable we will require object.

ex-
package Basics;

public class Test1 {

//Non-static or instance variable


double rollNo;
String c;

public static void main(String[] args) {


Test1 t=new Test1();
[Link]=15;

[Link]([Link]);

Test1 t1=new Test1();


[Link]=20;

[Link]([Link]);

Test1 t3=new Test1();


[Link]([Link]);
[Link](t3.c);

2) If there is no initalization of variable jvm will provide default values for


datatypes.

ex-
package Basics;

public class Test1 {

//Non-static or instance variable


double rollNo;
String c;

public static void main(String[] args) {

Test1 t3=new Test1();


[Link]([Link]);
[Link](t3.c);

}
o/p-
0.0
null

3) Value of instance variable will vary from object to object-

ex-package Variables;

public class InstanceVariable {


int x;

public static void main(String[] args) {

//Object1
InstanceVariable var=new InstanceVariable();

var.x=10;
[Link](var.x);

//Object2
InstanceVariable var1=new InstanceVariable();

[Link](var1.x);

}
o/p-
10
0
4) scope of the instance varible is throgh out the class.

2) local variable-

A variable which is declare locally(inside any curly braces or inside any method).

rule-

1) There is no need to create the object to access the local variable.


we can directly access the local variable.

ex-
package Variables;

public class LocalVariable {

public static void main(String[] args) {

//Local variables
int x=10;
[Link](x);

2) local variable shall be initalize at the time of declaration- else we will get
the compile time error
when we try to use the variable.

3) there will be no default values for local variable if it is not initalize.

4) scope of the local variable is with in block or braces or method where we have
declare it.

Practice example-

1) Gloabal variable-
Local variable

Transfer the vale of local to global-

ex-
package Variables;

public class LocalVariableClass {

//Class Level declaration


static int x;

public static void main(String[] args) {

[Link]("Class level Variable-value of x " + x);

//local variable
int x=10;

LocalVariableClass.x=x;

[Link]("After Assignment-Value of x " + LocalVariableClass.x);

}
o/p-
Class level Variable-value of x 12
After Assignment-Value of x 10

2) Transfer the value of global to local variable

ex-
package Variables;

public class LocalVariableClass {

//Class Level declaration


static int x=20;

public static void main(String[] args) {

[Link]("Class level Variable-value of x " + x);


//local variable

int x=LocalVariableClass.x;

[Link]("After Assignment-Value of x " + x);

}
o/p-
Class level Variable-value of x 20
After Assignment-Value of x 20

1) Declaration-
int x;

2) Initalization
x=10;

3) utilization

[Link](x)

main method declaration-

(access modifier) (type of method) (return type) (name of method) (argument) {


//Body

ex-
public static void main(String[] args) {
//body
}

Common questions

Powered by AI

Variable scoping determines the accessibility and lifetime of variables, thereby affecting a Java method's output. Static variables are accessible globally, instance variables are linked to specific objects, and local variables are restricted to the methods or blocks they are declared in. When methods access variables, the specific scope dictates which value is retrieved, thus impacting the output when the same variable name is used at different levels .

Static variables in Java are allocated memory when the class is loaded, remaining available throughout the program execution, and are shared among all instances of the class. This means if one instance modifies the static variable, the change is reflected across all instances. In contrast, instance variables are allocated memory on a per-object basis and each object maintains its own copy of the variable, allowing values to vary between objects .

Local variables in Java must be initialized before use because they do not have default values, unlike static and instance variables which are automatically initialized with default values if not explicitly set. Static and instance variables, therefore, do not require explicit initialization to hold default data types' values .

A programmer might use instance variables over static variables for properties that need to maintain uniqueness across multiple objects. Instance variables provide object-specific data storage, allowing each instance to maintain its own state without interference from other instances, contrasted with static variables which hold a single shared state across the class .

A compile-time error with local variables occurs if they are accessed without explicit initialization since they are not assigned default values. In contrast, static and instance variables do not face this issue as they are automatically initialized with default values corresponding to their data types, preventing such errors .

Default values allow instance variables to be used even without explicit initialization, as they automatically hold a default state correlated to their data types in Java. This helps prevent errors and ensures the program can rely on predictable initial states for these variables .

In Java, assigning a local variable to a global (static) variable demonstrates value transfer by allowing the local variable's value to be used beyond its typical scope. The static variable, which is accessible class-wide, retains the assigned value beyond the local block, showcasing how local changes can influence global state without needing to instantiate objects .

Local variables require declaration and initialization within the same block in Java to avoid compile-time errors since they lack default initialization and are scoped to the block they are declared in. Conversely, static and instance variables are class-level and object-level respectively, initialized with default values if not explicitly set, allowing them to be declared independently of their initialization .

When a static variable's value is changed in a Java program, all instances of the class reflect this new value because static variables are class-level properties shared among all objects of the class. This behavior underscores the stateless nature of static variables across different objects .

The scope of a static variable encompasses the entire class, allowing it to be accessed by all methods and objects of the class without instantiating an object. Instance variables have a scope limited to the objects they are associated with and can be accessed as long as the object exists. Local variables are limited in scope to the block or method in which they are defined, disappearing after the block or method concludes its execution .

You might also like