5 Replies Latest reply on Feb 15, 2007 3:41 PM by 807599

    Using this(); in a constructor

    807599
      I read an example that looked like this:
      class Body {
           public long idNum;
           public String name = "<unnamed>";
           public Body orbits = null;
           
           private static long nextID = 0;
           
           Body() {
                idNum = nextID++;
           }
      }
      then it adds:
      Body(String bodyName, Body orbitsAround) {
           this();
           name = bodyName;
           orbits = orbitsAround;
      }
      It said that the this(); caused the program to go back to the original constructor, make the object, then come back and continue. So, I am trying to do that with my code below, and the compiler is giving an error:
      cannot find symbol
      symbol  : constructor configComboBox()
      location: class configComboBox
                     this();
                     ^
      I dont know if I did the "extends" thing right, or if I can use that here. Or maybe that is a stupid thing to do in this situation.
      Thanks for the help.
      class configComponent {
           protected String variableName, Description, componentType;
           
           configComponent(String name, String type) {
                variableName = name;
                componentType = type;
           }
      }
      
      class configComboBox extends configComponent {
           private String[] valueList;
           
           configComboBox(String name, String[] List) {
                this();
                variableName = name;
                componentType  = "List";
                valueList = List;
           }
      }
        • 1. Re: Using this(); in a constructor
          796447
          Your class has no constructor taking no arguments (often referred to as the "default" constructor), so there is no this() to invoke.
          • 2. Re: Using this(); in a constructor
            807599
            Yes, I JUST realized that that would be an issue. I actually had this before and it gave the same error:
                 class configComboBox extends configComponent {
                      private String[] valueList;
                      
                      configComboBox(String name, String[] List) {
                           this(name, "List");
                           valueList = List;
                      }
                 }
            Is this any better?
            Thanks
            • 3. Re: Using this(); in a constructor
              796447
              this(name, "List");
              You have no constructor taking two String arguments either. You might think you do, but unlike methods, constructors are not inherited from base classes.

              If you want to invoke the base constructor, make that:
              super(name, "List");
              instead.
              • 4. Re: Using this(); in a constructor
                807599
                Ah, Perfect. That was excellent advice. Thank you very much!
                • 5. Re: Using this(); in a constructor
                  807599
                  By the way, I think this code:
                  class Body {
                      private long idNum;
                      private String name = "<unnamed>";
                      private Body orbits = null;
                       private static long nextID = 0;
                       
                       Body() {
                            idNum = nextID++;
                       }
                       
                       Body(String bodyName, Body orbitsAround) {
                            this();
                            name = bodyName;
                            orbits = orbitsAround;
                       }
                  }
                  Is slightly better written using an initialization block:
                  class Body {
                      private long idNum;
                      private String name = "<unnamed>";
                      private Body orbits = null;
                      private static long nextID = 0;
                  
                      //initialization block:
                      {
                          idNum = nextID++;
                      }
                  
                      Body(){}
                  
                      Body(String bodyName, Body orbitsAround) {
                          name = bodyName;
                          orbits = orbitsAround;
                      }
                  }
                  Why? If later you, or someone else, adds another constructor, you don't
                  have to remember to call this(); -- the initialization block automatically takes
                  care of that.