Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

INSTANCE MEMBER VARIABLE IN

JAVA

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INSTANCE MEMBER VARIABLES

• Instance variables store the data of an object


• Instance of a class: an object of the class
• The class declaration specifies the instance variables:
public class Counter

private int value;


}
• 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 variables are shared by all the instances of the class.

• Class methods are not tied to a specific object.

• 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:

Counter tally = new Counter();


tally.count();
tally.count();
int result = tally.getValue(); // Sets result to 2
• Each counter needs to store a variable that keeps track of how many times the counter has been
advanced

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
• An instance variable declaration consists of the following parts:

• access specifier (private)

• type of variable (such as int)

• name of variable (such as value)

• Each object of a class has its own set of instance variables

• You should declare all instance variables as private

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

• The count method advances the counter value by 1:

public void count()


{
value = value + 1;
}

• The getValue method returns the current value:

public int getValue()


{
return value;
}

• 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:

public class AClass {


public int instanceInteger = 0;
public int instanceMethod() {
return instanceInteger;
} public static int classInteger = 0;
public static int classMethod() {
return classInteger;
} public static void main(String[] args) {
AClass anInstance = new AClass();
AClass anotherInstance = new AClass(); //Refer to instance members through an instance.
anInstance.instanceInteger = 1;
anotherInstance.instanceInteger = 2;
System.out.println(anInstance.instanceMethod());
System.out.println(
anotherInstance.instanceMethod());
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
//Illegal to refer directly to instance members
//from a class method
//System.out.println(instanceMethod()); //illegal
//System.out.println(instanceInteger); //illegal
//Refer to class members through the class...
AClass.classInteger = 7;
System.out.println(classMethod()); //...or through an instance.
System.out.println(anInstance.classMethod()); //Instances share class variables
anInstance.classInteger = 9;
System.out.println(anInstance.classMethod());
System.out.println(anotherInstance.classMethod()); } }

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

Instance Member variables Static (class) variables


Instance variables are declared in a class, but outside a Class variables also known as static variables are declared with
method, constructor or any block. the static keyword in a class, but outside a method, constructor
or a block.
Instance variables are created when an object is created Static variables are created when the program starts and
with the use of the keyword 'new' and destroyed when the
object is destroyed. destroyed when the program stops.
Instance variables can be accessed directly by calling the
variable name inside the class. However, within static
methods (when instance variables are given accessibility), Static variables can be accessed by calling with the class
name ClassName.VariableName.
they should be called using the fully qualified
name. ObjectReference.VariableName.
Instance variables hold values that must be referenced by
more than one method, constructor or block, or essential There would only be one copy of each class variable per class,
parts of an object's state that must be present throughout the regardless of how many objects are created from it.
class.

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.

You might also like