Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

CO_IF_22412_CO2

Manisha Pokharkar, Lecturer, Vidyalankar Polytechnic

Date: 04 February 2021

<<MSBTE LEAD>>
Unit 02 :
Derived Syntactical
Constructs in Java

Written by

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Unit Outcome 2 :Identify
scope and lifetime of a
variable in the given program
code.

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Learning Outcome 2 : Student
should understand the concept of
scope of the variable.

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
What we will learn today

1. Types of variable Key takeaways


2. Scope and lifetime of variable Concept of variable and scope of variable.

Manisha Ashwin Pokharkar


Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar
Polytechnic, Mumbai

Page 5 Maharashtra State Board of Technical Education


Concept Map

Types of Variable

Local Variable Instance Variable Class/Static


Variable

Page 6 Maharashtra State Board of Technical Education


Learning Objective/ Key learning

► Understand types of Variable


► Understand concept of scope of variable

Page 7 Maharashtra State Board of Technical Education


Concept Explanation: Variable

► 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.

Page 8 Maharashtra State Board of Technical Education


Types of Java Variables

► Java allows the variables to be declared at any place or within any block.
► That is, in Java, we can declare the variables at many places, we can declare them either at the start of
the program or inside any classes, method/functions or inside the main method.
► Scope determines which variables are visible to other parts of your program and also what is the lifetime
of those variables.
► Depending upon the scope, visibility, and access to the variables, they can be classified under 3
categories.
The 3 types of variables in Java are –
1. Local Variables
2. Instance Variables
3. Static Variables

Page 9 Maharashtra State Board of Technical Education


Local Variables

► 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.
► Example:
for(int i = 10 ;i >= 1; i--)
{
// Body of for loop
}

In the above example, int i = 10 is a declaration of a local variable i. Its scope is only limited to the “for”
loop.
Page 10 Maharashtra State Board of Technical Education
Instance Variables

► 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.
► The instance variables are visible to all methods, constructors, and blocks in the class. Normally, it is recommended
that we should declare the instance with a “private” access specifier.
► We can declare instance variables inside a class. We can also declare them outside a method, block or constructor.
► Example:

Class AreaofShapes
{
//These are instance variables, present inside the class
double rectangleArea;
double squareArea;
double circleArea;
}

Page 11 Maharashtra State Board of Technical Education


Static Variables

► 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.
► Static variables are created at the start of program execution and get destroyed automatically after the
execution of the program.
► The initialization of static variables is not mandatory. If we do not initialize it with a value, it gets a
default value similar to the instance variables.
► Syntax of a Static Variable:
class ClassName{
static <DataType> <variable_name>;
}

Page 12 Maharashtra State Board of Technical Education


Java Variable Scope and Lifetime:

Variable Scope Lifetime

static Static variables apply to the class Exists for as long as the class it
as a whole and are declared belongs to is loaded in the JVM.
within the class but outside a See the Static Members lesson for
method. more information.
instance Instance variables apply to an Exists for as long as the instance of
instance of the class and are the class it belongs to.
declared within the class but
outside a method.
local Local variables apply to the Exists until the method it is
method they appear in. declared in finishes executing.
See the Method Scope lesson for
more information.

Page 13 Maharashtra State Board of Technical Education


Quick Revision

1) Types of Variable are__________


a) Local Variable
b) Global Variable
c) Static Variable
d) All the above

2) A variable which is declared inside a class, outside all the blocks and is declared as static is known as ___
a) class variable.
b) static variable
c) local variable
d) None

Page 14 Maharashtra State Board of Technical Education

You might also like