Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about this code

 
nadeem shake
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
nadeem shake
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think happens when this("unknown") is called?
 
nadeem shake
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Campbell Ritchie
Sheriff
Posts: 48909
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48909
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Ashish Dutt
    Ranch Hand
    Posts: 172
    Java MySQL Database Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Sheriff
    Posts: 48909
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You’re welcome.

    Please don’t quote so much of previous posts; only quote what you are specifically replying to.
     
    nadeem shake
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Jesper de Jong
    Java Cowboy
    Saloon Keeper
    Posts: 15272
    37
    Android IntelliJ IDE Java Scala Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    nadeem shake
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Now I get it thanks a million you've realy made my day, once again thank you !!!
     
    Campbell Ritchie
    Sheriff
    Posts: 48909
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Earlier, I wrote:While we are at it, please read your private messages.
    Do so. The messages are about something not optional.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic