Forums Register Login

a question about this code

+Pie Number of slices to send: Send

output:

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
+Pie Number of slices to send: Send
 

nadeem sk wrote:i need to know why do i have the last output as unknown and 0



What do you think it should be, and why?
+Pie Number of slices to send: Send
hii thanks for replying , i will surely follow your guidelines the next time i post,

ok according to me "Test29 d = new Test29(); " should call the last unparametrised constructor

public Test29()
{
this("unknown");
}

but as i am passing just "unknown" i dont understand why the 0 comes in salary.

+Pie Number of slices to send: Send
What do you think happens when this("unknown") is called?
+Pie Number of slices to send: Send
according to me when this("unknown") is called , it will in turn call the main constructor

public Test29(String n, int s)
{
name = n;
salary = s;
System.out.println("The name entered is ="+name+"and the salary is ="+salary);
}

and "unknown" gets passed into "String n" but i don't get what is passed to "int s"
+Pie Number of slices to send: Send
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.)
+Pie Number of slices to send: Send
 

nadeem sk wrote:according to me when this("unknown") is called , it will in turn call the main constructor

public Test29(String n, int s)



No, it doesn't.



+Pie Number of slices to send: Send
 

nadeem sk wrote:
output:

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.
+Pie Number of slices to send: Send
 

Ashish Dutt wrote: . . .

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.
    +Pie Number of slices to send: Send
     

    Campbell Ritchie wrote:

    Ashish Dutt wrote: . . .

    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.
    +Pie Number of slices to send: Send
    You’re welcome.

    Please don’t quote so much of previous posts; only quote what you are specifically replying to.
    +Pie Number of slices to send: Send
    But according to the code when i create the new object
    Test29 d = new Test29();

    it should call the constructor with no parameters which is this

    public Test29()
    {

    this("unknown");
    }

    somehow i fail to understand !!! please help
    1
    +Pie Number of slices to send: Send
    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.
    +Pie Number of slices to send: Send
    Now I get it thanks a million you've realy made my day, once again thank you !!!
    +Pie Number of slices to send: Send
     

    Earlier, I wrote:While we are at it, please read your private messages.

    Do so. The messages are about something not optional.
    You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1140 times.
    Similar Threads
    please check this code
    Casting problem
    Inheritence query??
    Polymorphism, arguments and instanceof questions
    Polymorphism example
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 07:50:36.