Results 1 to 3 of 3
  1. #1
    balla is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Question overloaded constructors, specifically the use of this(args...)

    In overloaded classes, to save space I know I can do this:

    Java Code:
    public class Test{
         //fields
    
         public Test(args...){
              //constructor code
         }
    
         public Test(){
              this(someOtherArgs...);
         }
    }
    is there a way to use the this(...) command not in the first line of the constructor? so instead it would read similar to:

    Java Code:
    ...
    
         public Test(){
              if(...) throw new Exception(...);
              this(...);
         }
    thanks!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    No. The call to "this" or "super" MUST be the first line of a constructor. Even if you don't write it, it is still there implicitly.
    Java Code:
    class Foo {
        Foo() {
            // code
        }
    }
    Since all objects inherit from Object, the above is actually
    Java Code:
    class Foo extends Object {
        Foo() {
            super(); //call Object constructor
            // code
        }
    }

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    If a constructor throws an exception anywhere in its body, the 'this' object will never be fully instantiated and your program will never get a reference to it. So don't worry about throwing something after calling the superclass constructor; you're not going to get a half-baked object. The only thing you need to worry about is if your constructor (or the super constructor) has side effects like the creation of other objects that aren't local or member variables... like, if your constructor puts something in a Collection that's accessible to other parts of the program, and then throws an exception. You should avoid those kinds of side effects anyway.

    Edit: I'm not sure what would happen if a constructor stored a reference to 'this' somewhere outside 'this', and then threw an exception. But again, constructors modifying unrelated objects or references is bad form.
    Last edited by kjkrum; 06-07-2011 at 09:06 AM.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2011, 06:29 PM
  2. can static methods be overloaded?
    By katturv in forum New To Java
    Replies: 2
    Last Post: 10-03-2010, 06:51 PM
  3. Args[0]
    By hakan123 in forum New To Java
    Replies: 11
    Last Post: 12-03-2009, 05:40 PM
  4. Overloaded method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:42 PM
  5. Overloaded constructor
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:42 PM

Tags for this Thread

Posting Permissions

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