You are on page 1of 20

Variables in Java

 Didal, Dianne S.
BSCS 3- Ipil Campus
Wh
at i
Var s a
ia b
le?
A variable is a named memory location that holds
the data value of a particular data type. A variable
in Java is a kind of container that contains the
value during program execution.

Variable is a basic unit of storage in a program that


represents reserved storage locations, whose values
can be manipulated during the execution of a
program. We assign a variable with a datatype.
Declaration of a Variable
To declare the variable, we must specify the
data type followed by the unique name of
the variable.
Syntax:
The declaration of a variable generally takes the
following syntax:
dataType variableName ;

Where dataType is a type-specifier which is any Java


data type and variableName is the unique name of a
variable. A variable name is an identifier, thus all the
naming conventions/rules of an identifier must be
applied for naming a variable.
Example:

double payRate ;

Here, payRate is a variable of double data


type.
Note: While naming the variables, make sure to follow the naming
conventions and rules of identifiers which are as follows:
1. The variable names cannot contain white spaces, for example,
long dist ance = 1000; is invalid because the variable name has a
space in it.
2. A variable name can begin with a special character dollar ($) and
underscore ( _ ).
3. The first letter of a variable cannot be a digit.
4. A variable name should begin with a lowercase letter, for
example, int number. For lengthy variable names having more than
one word, we can use camelCase, for example, int salaryPerDay;
float rateOfInterest; ,etc. are valid.
5. We cannot use keywords like int, for, while, class, etc as a variable
name.
6. Variable names are case-sensitive in Java.
Some
  other variable declarations are –
float area ;
char grade ;
String sentence ;
1. Local Variables in Java
A local variable in Java is a variable that we declare
inside a body of a method, block or a constructor.
We can use the local variable only within that
method and the other methods of the class are
unaware of the existence of this variable.
A block starts with an opening curly brace and ends
with a closing curly brace. The scope of a local
variable is restricted to a particular block. Its
lifetime is within the parenthesis in which it is
declared.
Syntax ofclass
a Local Variable:
ClassName
 
{
methodName(){
<DataType> localVariableName;
localVariableName = value;
}
}
Example:
 
double area()
{
int length=10; //local variable
int breadth = 5; //local variable
double areaOfRectangle = length * breadth; //local
variable
return areaOfRectangle;
} // scope of all local variables ends here.
2. Instance Variables in Java

A variable that is declared inside the class but outside


any method, constructor or block body is called an
instance variable. An instance variable is a non-static
variable that is, we can not declare it as static.

It is called an instance variable because its value is


instance-specific (related to objects) and is not shared
with other instances/objects as each object of the class
has its own set of values for these non-static variables.
Syntax of an Instance Variable:
class ClassName
{
<DataType> instanceVariableName;
instanceVariableName = value;
// class body
}
Example:
 
Class AreaofShapes
{
//These are instance variables, present inside the class
double rectangleArea;
double squareArea;
double circleArea;
}
3. Class Variables in Java
A variable that is declared inside a class but not
inside the method, constructor or a block, with the
static keyword is called static or class variable.

Static variables are also called class variables


because they are associated with the class and are
common for all the instances of the class. That is,
one copy of the static variable is shared among all
objects of the class.
Syntax of a Static Variable:

class ClassName{
static <DataType> <variable_name>;
}
Example:

class MyClass{
static int number; //number is a static variable
}
1-3.) What are the 3 types of variables?

4-5.) Two words that is broken down from the word


“variable”.
6.) The variable that is also called as Static Variable.
7.) The variable that is also called as Non-Static Variable.
8.) Every variable has a specific ___________.
9.) We first declare the datype before using it.
10.) Java variables is also called as _________ because
they are named.

You might also like