Reference Variable
Reference Variable
any access modifiers i.e. they are default access modifier , they are
}
Example
class Room class RoomArea
{ private float len;
{ p.s.v.m ( String args[ ])
private float breadth;
void getdata ( float a, float b) { float area;
{
Room room1 = new Room( );
len= a; breadth=b;
} room1.getdata( 14, 10);
} Output: RoomArea1.java:14: error: len
has private access in Room area= room1.len * room1.breadth;
area= room1.len * room1.breadth;
^
RoomArea1.java:14: error: breadth has S. o. p (“Area=“ +area);
private access in Room
area= room1.len * room1.breadth;
}
^
Use of ‘this’
class Test
class Main {
{ private float len; public static void main(String[] args) {
private float breadth; Test test = new Test();
void getdata ( float len, float breadth)
// set value of private variables
{
this.len= len; this.breadth=breadth; test.getdata(1,2);
} // get value of private variables
public float getlen()
System.out.println("Length " + test.getlen());
{
return this.len;
System.out.println("Breadth " +
}
test.getbreadth());
public float getbreadth() }
{ }
return this.breadth;
}
Length 1.0
} Breadth 2.0
• The this keyword refers to the current object in a method or
constructor parameter).
mybox is an instance of Box called ‘object’ . It has physical reality of
Box.
that contains its own copy of each instance variable defined by the
class.