aspose file tools*
The moose likes Beginning Java and the fly likes This in constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "This in constructor" Watch "This in constructor" New topic
Author

This in constructor

Thara Visu
Ranch Hand

Joined: May 17, 2005
Posts: 87
People,
Is it wrong or unacceptable to use the "this" reference inside a constructor?
Please let me know your responses.
Thanks in advance


Thara<br />SCJP 1.4 96%<br />SCBCD 1.3 96%
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
No, why should it be wrong or unacceptable?
Thara Visu
Ranch Hand

Joined: May 17, 2005
Posts: 87
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

Joined: Feb 20, 2007
Posts: 152
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

Joined: May 17, 2005
Posts: 87
Sid
I am not asking about this(). Lets say the below is an example
N Az
Greenhorn

Joined: Jan 28, 2007
Posts: 6
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

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

    Joined: Jan 29, 2003
    Posts: 8791
    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 ]

    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Thara Visu
    Ranch Hand

    Joined: May 17, 2005
    Posts: 87
    Thanks a lot guys... Was really helpful
    Peter Chase
    Ranch Hand

    Joined: Oct 30, 2001
    Posts: 1970
    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: -



    Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344


    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!!


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: This in constructor
     
    Similar Threads
    IS CONCTRUCTOR IS A METHOD
    main method
    Can I overload a constructor with enum values?
    WA #1.....word association
    Constructor doubt - K&B Book