0% found this document useful (0 votes)
4 views

Variable in Java

The document explains the concept of variables in Java, detailing three types: local, instance, and static variables. Local variables are confined to the method they are declared in, instance variables are tied to specific instances of a class, and static variables are shared across all instances of a class. Examples are provided to illustrate variable usage, including operations like widening and overflow.

Uploaded by

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

Variable in Java

The document explains the concept of variables in Java, detailing three types: local, instance, and static variables. Local variables are confined to the method they are declared in, instance variables are tied to specific instances of a class, and static variables are shared across all instances of a class. Examples are provided to illustrate variable usage, including operations like widening and overflow.

Uploaded by

dagah31071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Programming

Topperworld.in

variable in java

• A variable is a container which holds the value while the Java program is
executed. A variable is assigned with a data type.
• Variable is a name of memory location. There are three types of variables
in java: local, instance and static.
• A variable is the name of a reserved area allocated in memory. In other
words, it is a name of the memory location. It is a combination of "vary +
able" which means its value can be changed.

int data=10;//Here data is variable

©Topperworld
Java Programming

➔ Local Variable
• A variable declared inside the body of the method is called local
variable. You can use this variable only within that method and the
other methods in the class aren't even aware that the variable exists.
• A local variable cannot be defined with "static" keyword.

➔ Instance Variable
• A variable declared inside the class but outside the body of the method,
is called an instance variable. It is not declared as static.
• It is called an instance variable because its value is instance-specific and
is not shared among instances.

➔ Static variable
• A variable that is declared as static is called a static variable. It cannot
be local. You can create a single copy of the static variable and share it
among all the instances of the class. Memory allocation for static
variables happens only once when the class is loaded in the memory.

Example to understand the types of variables in java:

©Topperworld
Java Programming

public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class

Java Variable Example: Add Two Numbers

public class Simple{


public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}

Output: 20

©Topperworld
Java Programming

Java Variable Example: Widening

public class Simple{


public static void main(String[] args){
int a=10;
float f=a;
System.out.println(a);
System.out.println(f);
}}

10
Output:
10.0

Java Variable Example: Overflow

class Simple{
public static void main(String[] args){
//Overflow
int a=130;
byte b=(byte)a;
System.out.println(a);
System.out.println(b);
}}

130
Output:
-126

©Topperworld
Java Programming

Java Variable Example: Adding Lower Type

class Simple{
public static void main(String[] args){
byte a=10;
byte b=10;
//byte c=a+b;//Compile Time Error: because a+b=20 will be int
byte c=(byte)(a+b);
System.out.println(c);
}}

Output: 20

o Difference Between Local, Instance and Static Variables :

Sr. Local variables Instance variables Static variables


No.
1. Variables declared An instance variable is Static variables are
within a method are declared inside a class declared inside a class but
local variables. but outside of any outside of a method
method or block. starting with a keyword
static.
2. The scope of the local An instance variable is The static variable is
variable is limited to accessible throughout accessible throughout the
the method it is the class. class.
declared inside.

©Topperworld
Java Programming

3. A local variable starts The object associated The static variable has the
its lifetime when the with the instance same lifetime as the
method is invoked. variable decides its program.
lifetime.
4. Local variable is Instance variable has Static variables only have
accessible to all the different copies for one single copy of the
objects of the class. different objects. entire class.
5. Used to store values Used to store values Used for storing
that are required for that are needed to be constants.
a particular method. accessed by different
methods of the class.

©Topperworld

You might also like