Go Back
Question
Venu Gopal reddy Seelam
4 years ago
Answer
Read Mode
Answer posted by Anvesh Kanchibhotla
4 years ago
- In java, it is illegal to declare two local variables with the same name inside the same or enclosing scopes.
- But you can have formal parameters to methods, which overlap with the names of the class' instance variables.
- this keyword is used to refer to the current object.
- this can be used to resolve any name collisions that might occur between instance variables and formal variables.
- When a formal variable has the same name as an instance variable, the formal variable hides the instance variable.
- Also used in method chaining and constructor chaining.
// instance and formal variables are different
class Box{
double w=5,h=5,d=5;
Box(double w1,double h1,double d1){
w=w1;
h=h1;
d=d1;
}
double volume(){
return w*h*d;
}
}
class BoxTest1{
public static void main(String args[]){
Box b=new Box(1,2,3);
System.out.println("Volume is: " b.volume());
}
}
Output:
Volume is: 6.0
// instance and formal variables are same
class Box{
double w=5,h=5,d=5;
Box(double w,double h,double d){
w=w;
h=h;
d=d;
}
double volume(){
return w*h*d;
}
}
class BoxTest2{
public static void main(String args[]){
Box b=new Box(1,2,3);
System.out.println("Volume is: " b.volume());
}
}
Output:
Volume is:125.0
// 'this' hides the instance variables
class Box{
double w=5,h=5,d=5;
Box(double w,double h,double d){
this.w=w;
this.h=h;
this.d=d;
}
double volume(){
return w*h*d;
}
}
class BoxTest2{
public static void main(String args[]){
Box b=new Box(1,2,3);
System.out.println("Volume is: " b.volume());
}
}
Output:
Volume is:6.0
// method chaining
class Fchain{
int a,b;
Fchain setValue(int x,int y){
a=x;
b=y;
return this;
}
Fchain disp(){
System.out.println("a value is:" a);
System.out.println("b value is:" b);
return this;
}
}
class FchainDemo{
public static void main(String args[]){
Fchain f1=new Fchain();
f1.setValue(10,20).disp().setValue(11,22).disp();
}
}
//Constructor Chaining
class Test{
int a,b,c,d;
Test(int x,int y){
a=x;
b=y;
}
Test(int x,int y,int z){
this(x,y);
c=z;
}
Test(int p,int q,int r,int s){
this(p,q,r);
d=s;
}
void disp(){
System.out.println(a %u201D %u201D b %u201D %u201D c %u201D %u201D d);
}
}
class TestDemo{
public static void main(String args[]){
Test t1=new Test(10,20,30,40);
t1.disp();
}
}
Recommended Questions
Useful Files
Users Joined
ashutosh Kumar jha
2 days ago
Sahasra
3 days ago
Paman Induja Perera
1 week ago
Akram
1 week ago
praveen
1 week ago
X
EXPLAIN THIS KEYWORD IN JAVASCRIPT?
X
EXPLAIN THIS KEYWORD IN JAVASCRIPT?
- In java, it is illegal to declare two local variables with the same name inside the same or enclosing scopes.
- But you can have formal parameters to methods, which overlap with the names of the class' instance variables.
- this keyword is used to refer to the current object.
- this can be used to resolve any name collisions that might occur between instance variables and formal variables.
- When a formal variable has the same name as an instance variable, the formal variable hides the instance variable.
- Also used in method chaining and constructor chaining.
// instance and formal variables are different
class Box{
double w=5,h=5,d=5;
Box(double w1,double h1,double d1){
w=w1;
h=h1;
d=d1;
}
double volume(){
return w*h*d;
}
}
class BoxTest1{
public static void main(String args[]){
Box b=new Box(1,2,3);
System.out.println("Volume is: " b.volume());
}
}
Output:
Volume is: 6.0
// instance and formal variables are same
class Box{
double w=5,h=5,d=5;
Box(double w,double h,double d){
w=w;
h=h;
d=d;
}
double volume(){
return w*h*d;
}
}
class BoxTest2{
public static void main(String args[]){
Box b=new Box(1,2,3);
System.out.println("Volume is: " b.volume());
}
}
Output:
Volume is:125.0
// 'this' hides the instance variables
class Box{
double w=5,h=5,d=5;
Box(double w,double h,double d){
this.w=w;
this.h=h;
this.d=d;
}
double volume(){
return w*h*d;
}
}
class BoxTest2{
public static void main(String args[]){
Box b=new Box(1,2,3);
System.out.println("Volume is: " b.volume());
}
}
Output:
Volume is:6.0
// method chaining
class Fchain{
int a,b;
Fchain setValue(int x,int y){
a=x;
b=y;
return this;
}
Fchain disp(){
System.out.println("a value is:" a);
System.out.println("b value is:" b);
return this;
}
}
class FchainDemo{
public static void main(String args[]){
Fchain f1=new Fchain();
f1.setValue(10,20).disp().setValue(11,22).disp();
}
}
//Constructor Chaining
class Test{
int a,b,c,d;
Test(int x,int y){
a=x;
b=y;
}
Test(int x,int y,int z){
this(x,y);
c=z;
}
Test(int p,int q,int r,int s){
this(p,q,r);
d=s;
}
void disp(){
System.out.println(a %u201D %u201D b %u201D %u201D c %u201D %u201D d);
}
}
class TestDemo{
public static void main(String args[]){
Test t1=new Test(10,20,30,40);
t1.disp();
}
}
x
Loading...