• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This in constructor

 
Thara Visu
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People,
Is it wrong or unacceptable to use the "this" reference inside a constructor?
Please let me know your responses.
Thanks in advance
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, why should it be wrong or unacceptable?
 
Thara Visu
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Svend,
We had a small argument within our gang of friends.. and they were sayin that they have read somewhere that this shdnt be used in constructors.. So i just needed to confirm...
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor must have this(arguements, if any) or super(arguements, if any) as the first statement of any constructor, but not both. The VM inserts a no-arg super() for us. But if we use this(), then it doesn't.
 
Thara Visu
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sid
I am not asking about this(). Lets say the below is an example
 
N Az
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The use of this is fully qualified. I even wrote a small program to print the values and it is working fine.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Svend Rost that this is permissible in a constructor.

The snippet you quote with School.addStudent(this); is legal syntax, but has several potential bad bits of programming in:
  • The constructor is for setting up the Student object, not for updating the School object, which is what you are doing here.
  • You should be adding to the School in the School object.
  • Better
    Now, there are two possible uses of "this" in a Java constructor. One is where you have several overloaded constructors, and you use "this" to call the other constructors. Example (amended from Deitel and Deitel 6th Edition page 366f)As Sidd Cool has said, you can only use this( . . . ); or super( . . . ); as the first statement in the constructor, so you can't use both in the same constructor.

    As for using this. in a constructor, well there are several ways to set up a constructor.

    BEWARE: Two of the constructors shown below contain serious logic errors. I shall let the readers work out which.
    Let's look at this class:-Type 1:Type 2:Type 3:Type 4:Type 5Type 1 has the drawback that you chose a proper name for the fields, and in the public interface of the class you are exposing the useless little names of "p" and "b." In 2 and 3 you have actually get a better name for the initial balance. Type 4 is what you would get from the Eclipse IDE if you set up the fields, then click Source->Create Constructors from Fields.

    I think the best form for this particular constructor would be this sort of thing:-
    I hope this goes some way to answering your query. CR
    [Edited for minor spelling and formatting errors]
    [ February 26, 2007: Message edited by: Campbell Ritchie ]
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Calling on a dim memory that says "Don't do that!" I think the little example

    is a dangerous practice because we are not guaranteed that "this" is completely set up and safe to use until the constructor returns. School might start calling methods on the Student before Student is fully initialized.

    What if somebody extends Student?

    School could call Student methods before StarStudent is ready. The short guideline is don't expose yourself before you're fully dressed.
    [ February 26, 2007: Message edited by: Stan James ]
     
    Thara Visu
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot guys... Was really helpful
     
    Peter Chase
    Ranch Hand
    Posts: 1970
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't think anyone has mentioned the situation in which trying to use "this" (explicitly or implicitly) in a constructor is illegal (compile error).

    That situation is where you try to use "this" inside a call to superclass constructor. The compiler won't let you do it.

    For instance: -

     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    School could call Student methods before StarStudent is ready. The short guideline is don't expose yourself before you're fully dressed.


    Thats perfect!! I would go with Stan James!!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic