The name entered is =ABCand the salary is =0
The name entered is =unknownand the salary is =1000
The name entered is =xyzand the salary is =2000
The name entered is =unknownand the salary is =0
i need to know why do i have the last output as unknown and 0
Get a pencil and some paper and write all those constructors down. Then draw arrows showing which constructor is called from which place. Remember there is a particular order for calling constructors, which you can find in the Java Language Specification. (I am not sure I have quoted the correct section, however.)
The name entered is =ABCand the salary is =0
The name entered is =unknownand the salary is =1000
The name entered is =xyzand the salary is =2000
The name entered is =unknownand the salary is =0
i need to know why do i have the last output as unknown and 0
Of what i have understood of the keyword "this" is that it is used inside any method to refer to the current object. Since its illegal in Java to have two variables with the same name within the same scope, however you can have the local variables (the ones that are declared in the method) named same as the instance variables declared within the class scope (but outside the method). But this approach leads to the problem wherein the local variable would hide the instance variable. which is logical too.
And because the keyword this lets you refer directly to the object you can resolve any namespace collisions that might occur between instance variables and local variables.
If my understanding of Java so far is correct, then I would say Constructor is like a method. Based on this analogy if you look at the code again "this("unknown")" you are passing a string argument to the this constructor(0r method) and this method signature matches the following constructor signature
Hence it prints the output as unknown and 0.
Of what i have understood of the keyword "this" is that it is used inside any method to refer to the current object. Since its illegal in Java to have two variables with the same name within the same scope, however you can have the local variables (the ones that are declared in the method) named same as the instance variables declared within the class scope (but outside the method). But this approach leads to the problem wherein the local variable would hide the instance variable. which is logical too.
And because the keyword this lets you refer directly to the object you can resolve any namespace collisions that might occur between instance variables and local variables.
If my understanding of Java so far is correct, then I would say Constructor is like a method. Based on this analogy if you look at the code again "this("unknown")" you are passing a string argument to the this constructor(0r method) and this method signature matches the following constructor signature . . .
I am afraid your understanding is incorrect.
1: Constructors are not methods. There is a superficial resemblance in the code, however.
2: this(...); has a specific meaning, and can only be used as the first line of a constructor.
3: You do not use this() to distinguish local variables from fields. You use this.xxx Don’t call it a namespace collision; it is called shadowing.
The problem is that OP is not correctly following the many constructors. I think that you can print all those constructors on a sheet of paper and follow the path of execution by drawing arrows. I think you have correctly identified which constructor is called.
Of what i have understood of the keyword "this" is that it is used inside any method to refer to the current object. Since its illegal in Java to have two variables with the same name within the same scope, however you can have the local variables (the ones that are declared in the method) named same as the instance variables declared within the class scope (but outside the method). But this approach leads to the problem wherein the local variable would hide the instance variable. which is logical too.
And because the keyword this lets you refer directly to the object you can resolve any namespace collisions that might occur between instance variables and local variables.
If my understanding of Java so far is correct, then I would say Constructor is like a method. Based on this analogy if you look at the code again "this("unknown")" you are passing a string argument to the this constructor(0r method) and this method signature matches the following constructor signature . . .
I am afraid your understanding is incorrect.
1: Constructors are not methods. There is a superficial resemblance in the code, however.
2: this(...); has a specific meaning, and can only be used as the first line of a constructor.
3: You do not use this() to distinguish local variables from fields. You use this.xxx Don’t call it a namespace collision; it is called shadowing.
The problem is that OP is not correctly following the many constructors. I think that you can print all those constructors on a sheet of paper and follow the path of execution by drawing arrows. I think you have correctly identified which constructor is called.
Thank you for the correction. Appreciated. esp the detailed response.
As people have already told you, that is indeed what happens.
Line 42: Create a new Test29 object. The no-args constructor (lines 27 - 31) is called.
Line 30: this("unknown"); calls the constructor that takes one parameter, a string. That's the constructor in lines 15 - 19.
Line 18: this(n,0); calls the constructor that takes two parameters; a string and an int. That's the constructor in lines 6 - 12.
Line 9: name is set to what was passed from the other constructors on lines 30 and 18: "unknown".
Line 10: salary is set to what was passed from the other constructor on line 18: 0.