0% found this document useful (0 votes)
4 views12 pages

Lecture 3.1 - Variable Types

The document outlines the different types of variables in Java, categorizing them into local variables, class variables, and instance variables. It explains their scope, memory representation, and accessibility, providing examples for clarity. Additionally, it includes references to various Java programming books for further reading.

Uploaded by

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

Lecture 3.1 - Variable Types

The document outlines the different types of variables in Java, categorizing them into local variables, class variables, and instance variables. It explains their scope, memory representation, and accessibility, providing examples for clarity. Additionally, it includes references to various Java programming books for further reading.

Uploaded by

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

Variable Types

Course Code: CSC1205 Course Title: Object Oriented Programming 1 (JAVA)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 3.1 Week No: 3 Semester: Fall 24-25

Lecturer: Raima Adhikary, [email protected]


Lecture Outline

1. Different Types of Variables.


2. Memory Representation of Different Types of variables.
Variable Types
Different Types of Variables

In Java, Variables can be divided into 3 types. They are:

Local Variables

Class Variables

Instance Variables
Variable Types
Different Types of Variables

Local Variables
• Variables declared inside the scope of any method, constructor, loop, conditional statements
are known as local variables.
• Local variables do not have any existence outside the scope it is declared.
• The concept of default value is not applicable for local variables.
Example A list of local variables declared
in the example is:
public void changeValues(int a){  a and x is accessible/exists
int x; • int a
• int x throughout the whole
for(int i=0; i<5; i++){ method.
int y = i + a; • int i  i and y is accessible/exists
if(y%2 ==0){ • int y
only within the for loop.
int z = a * y; • int z  z is only accessible/exists
} These variables do not have any inside the if block.
} existence outside the block they
} were declared.
Variable Types
Different Types of Variables

Class Variables
• Variables declared inside the scope of a class but outside the scope of any method
or constructor are known as Class Variables if and only if the keyword static is
used in the declaration of the variable.
• It can be accessed from anywhere inside the class it is declared.
• It can also be accessed from outside of the class it is declared depending its access
modifiers.
• We do not need any object to access it. It is accessed using the class name.
• All the objects of a class, share the same memory for a class variable.

Example and memory representation is given in a later slide.


Variable Types
Different Types of Variables

Instance Variables
• Variables declared inside the scope of a class but outside the scope of any method
or constructor are known as Class Variables if and only if the keyword static is not
used in the declaration of the variable.
• It can be accessed from anywhere inside the class it is declared.
• It can also be accessed from outside of the class it is declared depending its access
modifiers.
• We have to use an object to access it.
• Each of the objects of a class, hold different memory for an instance variable.

Example and memory representation is given in a the next slide.


Variable Types
Example of Different Types of Variables

class Account{ Can you identify which ones are local


private int accountNo; variables, which ones are class variables
private double balance; and which ones are instance variables?
public static double perDayTransactionLimit;

public Account( ){ } Local Variable an, b, rate


public Account(int an, double b){ Class Variable perDayTransactionLimit
accountNo = an; balance = b;
} Instance Variable accountNo, balance
public void addInterest(double rate){
balance = balance + (balance * rate / 100);
}
public void show( ){
System.out.println(“AccountNo: ”+ accountNo);
System.out.println(“Balance: ”+ balance);
}
}
Variable Types
Memory Representation of Different Types of Variables

class Account{ Lets assume that we are creating two


private int accountNo; objects of this account class:
private double balance; Account a1 = new Account(1111, 200.0);
public static double perDayTransactionLimit= 500;
Account a2 = new Account(1112, 250.0);
public Account( ){ }
What will be the memory representation
public Account(int an, double b){
for an object of account?
accountNo = an; balance = b;
}
public void addInterest(double rate){ accountNo 1111 accountNo 1112
balance = balance + (balance * rate / 100);
balance 200.0 balance 250.0
}
public void show( ){ a2
a1
System.out.println(“AccountNo: ”+ accountNo);
System.out.println(“Balance: ”+ balance); perDayTransactionLimit 500
}
}
Variable Types
Memory Representation of Different Types of Variables

class Account{ What will be the memory representation


private int accountNo; for another object of account?
private double balance; Account a3 = new Account(1113, 300.0);
public static double perDayTransactionLimit= 500;

public Account( ){ } accountNo 1111 accountNo 1112


public Account(int an, double b){
accountNo = an; balance = b; balance 200.0 balance 250.0
}
a1 a2
public void addInterest(double rate){
balance = balance + (balance * rate / 100); perDayTransactionLimit 500
}
public void show( ){
a3
System.out.println(“AccountNo: ”+ accountNo);
System.out.println(“Balance: ”+ balance);
accountNo 1113
}
} balance 300.0
Variable Types
Memory Representation of Different Types of Variables

class Account{ What will be the memory representation


private int accountNo; for a4 after the following statement?
private double balance; Account a4 = a2;
public static double perDayTransactionLimit= 500;

public Account( ){ } accountNo 1111 accountNo 1112


public Account(int an, double b){
accountNo = an; balance = b; balance 200.0 balance 250.0
}
a1 a2
public void addInterest(double rate){
balance = balance + (balance * rate / 100); perDayTransactionLimit 500
}
public void show( ){
a3 a4
System.out.println(“AccountNo: ”+ accountNo);
System.out.println(“Balance: ”+ balance); No New Memory
accountNo 1113
} for a4
} balance 300.0
Books

• Java Complete Reference, 7th Edition, By Herbert Schildt.


• A Programmer’s Guide to Java™ SCJP Certification A Comprehensive Primer,
3rd edition, by Khalid A. Mughal and Rolf W. Rasmussen
• Java How to Program Java, 9th Edition, By Deitel and Deitel.
• The Java Language Specification, By J. Gosling, B. Joy, G. Steele, G.Bracha and
A. Buckley
• Introduction to Programming Using Java, 6th Edition, By David j. Eck
• Head First Java, By Kathy Sierra and Bert Bates
References

• Java Complete Reference, 7th Edition, By Herbert Schildt.


• A Programmer’s Guide to Java™ SCJP Certification A Comprehensive Primer,
3rd edition, by Khalid A. Mughal and Rolf W. Rasmussen
• The Java Language Specification, By J. Gosling, B. Joy, G. Steele, G.Bracha and
A. Buckley
• docs.oracle.com

You might also like