Java-Variables_about_different_Scope.ppt

JyothiAmpally 43 views 17 slides Aug 05, 2024
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

Scope of Java Variables


Slide Content

●This module is intend to develop basic skills in
working with different types of variables that java
supports.
●Clearly differenciate the advantages and
disadvantages of working with different types of
variables.
Module Overview
2

Use different types of variables in a program
Participant should be able to identify the type of variable
to use according to the program needs.
Understand between static and non-static variables.
Undestand the memory allocations to different variables
in java.
Understand the working of primitive variable and reference
variable
Declare variables of class type
Construct an object using new
Describe default initialization
Describe the significance of a reference variable
State the consequences of assigning variables of class type
Objectives
3

Variable
Variable:A variable refers to a memory location with some value.
These memory locations are identified by identifiers. Amount of
memory allocation to the variables are decided by the data type
of the variable.
Syntax:
type identifier [ = value] ;
Example:
int weight=40;
float salary=22222.0f
String name = “Taksheel”
4

Java supports 3 types of variables .
Local variables
Instance variables
Class/static variables
Types of Variables
5

Local variables :
Local variables are declared in methods,
constructors, or blocks.
Local variables are created when the method,
constructor or block is entered and the variable
will be destroyed once it exits the method,
constructor or block.
Access modifiers cannot be used for local
variables.
Local variables are visible only within the
declared method, constructor or block.
Local Variables

Instance variables are declared in a class, but
outside a method, constructor or any block.
Memory is allocated to them when object is
created and initialised with default values or
literals values.
Instance variables are accessed directly by
methods, constructors, blocks, or essential
parts of an object.
Instance variables can be declared in class
level which are called as static variables.
Instance Variables
7

Class variables also known as static variables are
declared with the static keyword in a class, but
outside a method, constructor or a block.
There would be only one copy ofstatic variable across
all objects of a single class.
Static variables are stored in static memory. Normally
static variables are declared public /private and
final.Which then function as constants.
Class or Static Variables
<<Module name>>/<<Session #>>/<<Lesson Name>> >>
8

• Static variables are created when the program starts
and destroyed when the program stops.
• Visibility and Default values are similar to instance
variables. Static values can be assigned in static
initializer blocks.
• Static variables can be accessed by calling with the
class name . ClassName.VariableName.
Class or Static Variables(Cont…)
<<Module name>>/<<Session #>>/<<Lesson Name>> >>
9

Example Local Variable
public class Test{
public void machineAge(){
int age = 0;//local variable
age = age + 7;
System.out.println(“Machine age is : " + age)
}

public static void main(String args[]){
Test test = new Test();//test is an instance of Test class
Test.machineAge();
}
}
10

Example Instance Variable
<<Module name>>/<<Session #>>/<<Lesson Name>> >>
11
public class Machine{
String mname;//instance variable
float mcost;//instance variable
public float machineCost(){
float vat= 7/100;//local variable
this.mcost= mcost + vat;
System.out.println(“Machine age is : " + age)
}
public static void main(String args[]){
Machine sewing = new Machine();//machine is an instance of
Machine class
System.out.println(“Machine Cost:”+ sewing.machineCost());
}
}

Example Static Variable
public class Machine{
String mname;//instance variable
float mcost;//instance variable
static String manu_name;//static variable
public float machineCost(){
float vat= 7/100;//local variable
this.mcost= mcost + vat;
System.out.println(“Machine age is : " + age)
}
}
<<Module name>>/<<Session #>>/<<Lesson Name>> >>
12

Static Variable(Cont…)
Class MachineApps{
public static void main(String args[]){
Machine sewing M= new Machine();//sewing is an instance of
Machine class
Machine.manu_name=“Usha”;
System.out.println(“Machine Cost:”+ sewingM.machineCost());
Machine fan= new Machine();/
System.out.println(“Machine manu Name:”+
sewingM.manu_name);
System.out.println(“Machine manu Name:”+ fan. manu_name);
}
}
<<Module name>>/<<Session #>>/<<Lesson Name>> >>
13

Scope Example
public class ScopeExample {
private int i=1;
public void firstMethod() {
int i=4, j=5;
this.i = i + j;
secondMethod(7);
}

Scope Example(Cont…)
public void secondMethod(int i) {
int j=8;
this.i = i + j;
}
}
public class TestScoping {
public static void main(String[] args) {
ScopeExample scope = new ScopeExample();
scope.firstMethod();
}
}

Allocation of Memory
Variables i,j are
local variables
which have
different values
in methods.
These variables
are declared in
method stack.
InstanceVariable i
is declared in
heap.

Thank You