File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes a question about this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a question about this code" Watch "a question about this code" New topic
Author

a question about this code

nadeem shake
Greenhorn

Joined: May 01, 2013
Posts: 9

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 01, 2013
Posts: 9
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
Marshal

Joined: Mar 22, 2005
Posts: 42912
    
  68
What do you think happens when this("unknown") is called?
nadeem shake
Greenhorn

Joined: May 01, 2013
Posts: 9
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

Joined: Oct 13, 2005
Posts: 40034
    
  28
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 12, 2013
Posts: 172

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.


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
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

    Joined: Apr 12, 2013
    Posts: 172

    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

    Joined: Oct 13, 2005
    Posts: 40034
        
      28
    You’re welcome.

    Please don’t quote so much of previous posts; only quote what you are specifically replying to.
    nadeem shake
    Greenhorn

    Joined: May 01, 2013
    Posts: 9
    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

    Joined: Aug 16, 2005
    Posts: 14428
        
      23

    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.

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    nadeem shake
    Greenhorn

    Joined: May 01, 2013
    Posts: 9
    Now I get it thanks a million you've realy made my day, once again thank you !!!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40034
        
      28
    Earlier, I wrote:While we are at it, please read your private messages.
    Do so. The messages are about something not optional.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: a question about this code