Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Multiple Constructors without using "this"

  1. #1
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Multiple Constructors without using "this"

    Hello guys,

    I have a question, I have this constructor:

    Java Code:
    	public Point(int horizontal, int vertical, double facthor, double factver) {
    		this.horizontal = horizontal;
    		this.vertical = vertical;
    		this.facthor = facthor;
    		this.factver = factver;
    	}
    If I want another constructor with default "facthor" and "factver" has 1.0, I can do this:

    Java Code:
    public Point(int horizontal, int vertical) {
    this(horizontal, vertical,1.0 ,1.0)
    }
    But I don't want to use this, so can I do something like this:

    Java Code:
    public Ponto(int horizontal, int vertical) {
    		new Ponto(horizontal, vertical, 1.0, 1.0);
    	}
    ??

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Multiple Constructors without using "this"

    Quote Originally Posted by tcms View Post
    But I don't want to use this, so can I do something like this:

    Java Code:
    public Ponto(int horizontal, int vertical) {
    		new Ponto(horizontal, vertical, 1.0, 1.0);
    	}
    ??
    No, you can't and shouldn't do that. The solution is to just use "this" as per your first example. Why on earth would you want to not use this feature?!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Multiple Constructors without using "this"

    Nope, that can't be done in Java; the language simply doesn't allow it; in your example another Ponto will be created which is not what you want. What's against the use of the word 'this' anyway?

    kind regards,

    Jos

    edit: jeeez, Fubarable is back again and immediately he makes me look like a slow old sod again; thanks for that, Fubarable ;-)
    Last edited by JosAH; 06-04-2013 at 08:32 PM.
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Multiple Constructors without using "this"

    I really don't need to "don't" use this feature. I just make the question to learn more about it, and why I shouln't do that way.

    I just wanted to know if there is an alternative to "this", just to know! although I use "this" feature everytime!
    Thanks to both!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Multiple Constructors without using "this"

    Quote Originally Posted by JosAH View Post
    edit: jeeez, Fubarable is back again and immediately he makes me look like a slow old sod again; thanks for that, Fubarable ;-)
    You've never needed my help for this.

    Kinder regards,

    Fu

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. add multiple "undo" to stack calc????
    By Bgreen7887 in forum New To Java
    Replies: 0
    Last Post: 03-30-2011, 09:04 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •