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

2.java Variables

A variable is a name given to a memory location that can store a value. In Java, variables must be declared before use with a datatype and name. There are three types of variables: local variables defined within a method, instance variables declared in a class, and static variables declared using the static keyword. Local variables are created and destroyed within the block they are declared, instance variables are created and destroyed with the object, and static variables exist for the class lifetime regardless of objects.

Uploaded by

Vicky Vignesh
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
0% found this document useful (0 votes)
104 views6 pages

2.java Variables

A variable is a name given to a memory location that can store a value. In Java, variables must be declared before use with a datatype and name. There are three types of variables: local variables defined within a method, instance variables declared in a class, and static variables declared using the static keyword. Local variables are created and destroyed within the block they are declared, instance variables are created and destroyed with the object, and static variables exist for the class lifetime regardless of objects.

Uploaded by

Vicky Vignesh
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1/ 6

Variables in Java:

A variable is a name given to a memory location. It is the basic unit of storage


in a program.
· The value stored in a variable can be changed during program execution.
· A variable is only a name given to a memory location, all the operations
done on the variable effects that memory location.
· In Java, all the variables must be declared before use.

Syntax of variable:
datatype variable_name = value;
Example: int num = 10;

datatype: Type of data that can be stored in this variable.


variable_name: Name given to the variable.
value: It is the initial value stored in the variable.

Types of variables:
There are three types of variables in Java:
· Local Variables
· Instance Variables
· Static Variables

1.Local Variables:
· A variable defined within a block or method or constructor is called local
variable.
· These variable are created when the block in entered or the function is
called and destroyed after exiting from the block or when the call returns
from the function.
· The scope of these variables exists only within the block in which the
variable is declared. i.e. we can access these variable only within that block.
· Initialization of Local Variable is Mandatory.

Sample Program :
public class StudentDetails {
public void studentAge()
{
// local variable age
int age = 0;
age = age + 5;
System.out.println("Student age is : " + age);
}

public static void main(String args[])


{
StudentDetails obj = new StudentDetails();
obj.studentAge();
}
}

2.Instance Variables:
· Instance variables are non-static variables and are declared in a class
outside any method, constructor or block.
· As instance variables are declared in a class, these variables are created
when an object of the class is created and
destroyed when the object is destroyed.
· Unlike local variables, we may use access specifiers for instance variables. If
we do not specify any access specifier
then the default access specifier will be used.
· Initilisation of Instance Variable is not Mandatory. Its default value is 0
· Instance Variable can be accessed only by creating objects.
Sample Program:
import java.io.*;
class Marks {
// These variables are instance variables.
// These variables are in a class
// and are not inside any function
int engMarks;
int mathsMarks;
int phyMarks;
}

class MarksDemo {
public static void main(String args[])
{
// first object
Marks obj1 = new Marks();
obj1.engMarks = 50;
obj1.mathsMarks = 80;
obj1.phyMarks = 90;

// second object
Marks obj2 = new Marks();
obj2.engMarks = 80;
obj2.mathsMarks = 60;
obj2.phyMarks = 85;

// displaying marks for first object


System.out.println("Marks for first object:");
System.out.println(obj1.engMarks);
System.out.println(obj1.mathsMarks);
System.out.println(obj1.phyMarks);

// displaying marks for second object


System.out.println("Marks for second object:");
System.out.println(obj2.engMarks);
System.out.println(obj2.mathsMarks);
System.out.println(obj2.phyMarks);
}
}

3.Static Variables:
· Static variables are also known as Class variables.
· These variables are declared similarly as instance variables, the difference is
that static variables are declared using the static keyword within a class
outside any method constructor or block.
· Unlike instance variables, we can only have one copy of a static variable per
class irrespective of how many objects we create.
· Static variables are created at the start of program execution and destroyed
automatically when execution ends.
· Initilisation of Static Variable is not Mandatory. Its default value is 0
· If we access the static variable like Instance variable (through an object), the
compiler will show the warning message and it won’t halt the program. The
compiler will replace the object name to class name automatically.
· If we access the static variable without the class name, Compiler will
automatically append the class name.

To access static variables, we need not create an object of that class, we can
simply access the variable as
class_name.variable_name;

Sample Program:
import java.io.*;
class Emp {

// static variable salary


public static double salary;
public static String name = "Harsh";
}

public class EmpDemo {


public static void main(String args[])
{

// accessing static variable without object


Emp.salary = 1000;
System.out.println(Emp.name + "'s average salary:"
+ Emp.salary);
}
}

Example:
class StudentDetails {

// global variable
int age ;

//static variable
static String name;

public int studentAge()


{
//local variable
int a =5;
System.out.println("inside method: " + a);
return a;
}
public static void main(String args[])
{
StudentDetails obj = new StudentDetails();
obj.age = 20;

int a1 = obj.studentAge();
int a3= obj.age + a1;
System.out.println(a3);

StudentDetails.name = "Rajesh";
StudentDetails.name = "Suresh";
System.out.println("Name of Student: " + StudentDetails.name );
}
}

DataTypes:

Primitive datatypes:
1. number declaration - int , long
2.decimal numbers declaration - float , double
3. Boolean declaration(true, false) - boolean
4.Characters declaration('j','2','@') - char

Class Level datatypes:


1. word ,name declaration("kumar","123",) - String
2. number declaration - Integer ,Long
3.decimal numbers declaration - Float , Double, BigDecimal
4. Boolean declaration- Boolean
5.Characters declaration - Character

You might also like