class order
{
int oid;
order (int oid)
{
oid=oid;
}
void displayorder()
{
System.out.println("order number :"+oid);
}
}
class executeorder {
public static void main(String[] args) {
order o = new order(1001);
o.displayorder();
}
}
A)
Output:
order number :0
->the reason for the un expected output is the “name clash” between the instance variable and the parameter(local variable) of the constructor (i.e. oid).
->we can resolve the name clash by using “this” keyword.
Ex:
Order(int oid)
{
This.oid=oid;
}
Output:
order number :1001
order number :1001
->”this” is a keyword in java which acts as an “implicit reference” to the current object.
Comments
Post a Comment