Professional Documents
Culture Documents
Session 1.12 Instance Member Variable
Session 1.12 Instance Member Variable
JAVA
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INSTANCE MEMBER VARIABLES
}
• State variables are also called instance variables or fields.
• Roughly speaking, the private modifier means that the variables are not part of the object’s interface.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
• Instance variables belong to a specific instance.
• Instance methods are invoked by an instance of the class.
Class Variables, Constants, and Methods
• Class constants are final variables shared by all the instances of the class.
• To declare class variables, constants, and methods, use the static modifier.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
• Example: tally counter
• Simulator statements:
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
• An instance variable declaration consists of the following parts:
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
The following figure shows the objects and member variables in the
program and how they are related.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Instance Member Variable Declaration
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Accessing Instance Member Variables
• Private instance variables can only be accessed by methods of the same class
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Instance Variables
1. Encapsulation is the process of hiding object data and providing methods for data access
2. To encapsulate data, declare instance variables as private and declare public methods that access
the variables
3. Encapsulation allows a programmer to use a class without having to know its implementation
4. Information hiding makes it simpler for the implementor of a class to locate errors and change
implementations
5. Protects your data (get/set methods access or/mutator methods) from others and from yourself!!!
6. How else could the class counter be implemented instead of using int ???? (float, string…)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example:
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Here's the output from the program:
1
2
7
7
9
9
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Difference between Instance and Static Varaibles
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example for Instance and static variable:
public class VariableExample{
int myVariable;
static int data = 30;
public static void main(String args[])
{
VariableExample obj = new VariableExample();
System.out.println("Value of instance variable: "+obj.myVariable);
System.out.println("Value of static variable: "+VariableExample.data);
}}
Output:
Value of instance variable: 0
Value of static variable: 30
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
MCQs
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
1.What is the output for this program?
class Inst{
A) 10 10
int x = 10; B) 20 20
public static void main(String[] args) C) 10 20
D) 20 10
{
Inst t1 = new Inst();
Inst t2 = new Inst();
t1.x = 20;
System.out.print(t1.x + " ");
System.out.println(t2.x);
}
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer:
d) 20 10
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
2.What is the output of this question?
A) 3 6 9
class Inst1 {
B) 3 6 9 …. 27
static int i = 1; C) Error
public static void main(String[] args) D) none
{
for (int i = 1; i < 10; i++) {
i = i + 2;
System.out.print(i + " ");
}
}
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer:
A) 3 6 9
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
3.What is the output of this question?
class Inst2{ A) 1 3 9
B) 1 2 3 … 9
static int i = 1;
C) 3 5 7 9 11 13 15 17 19
public static void main(String[] args) D) None
{
int i = 1;
for (Inst2.i = 1; Inst2.i < 10; Inst2.i++) {
i = i + 2;
System.out.print(i + " ");
}
}
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer:
C) 3 5 7 9 11 13 15 17 19
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
4.What is the output of this question?
A)Error
class Test1 {
B)1 3 9
static int i = 1; C)3 5 7 9 11 13 15 17 19
public static void main(String[] args) D)1 2 3 … 9
{
static int i = 1;
for (Test1.i = 1; Test1.i < 10; Test1.i++) {
i = i + 2;
System.out.print(i + " ");
}
}
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer:
A)Error
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
5.What is the output of this question?
class Arr1{
public static void main(String[] args) A)Error
B)5 5
{
C)5 3
static int arr1[] = { 11, 22, 33 }; D)3 5
static int arr2[] = { 11, 22, 33, 44, 55 };
static int ptr[];
ptr = arr1;
arr1 = arr2;
arr2 = ptr;
System.out.print(arr1.length + " ");
System.out.println(arr2.length);
}}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer:
A)Error
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Thank You!!!!
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.