Can I call default constructor from parameterized constructor inside public class in java?

后端 未结 6 1329
暗喜
暗喜 2021-02-19 22:44

I want to call default constructor from a parameterized constructor inside a public java class.

Can I achieve it?

相关标签:
6条回答
  • 2021-02-19 22:50

    Use this(); in the first line of the parametrized constructor and it will call your default constructor. Make sure you have default constructor as compiler will not provide one if you declare a parametrized constructor.

    0 讨论(0)
  • 2021-02-19 22:55

    You can't call a default constructor once you've created a constructor that takes arguments. You'll have to create the no argument constructor yourself in order to make a call from the parameterized constructor.

    0 讨论(0)
  • 2021-02-19 22:58

    You can just call default constructor with new operator (like this: new Test();) or this();. just Test() is forbidden because its not a method of class.

    package org.gpowork.test;
    
    public class Test {
        private String field;
        private Long time = 0L; 
        public Test(){
            this.time = System.currentTimeMillis();
            System.out.println("Default constructor. "+this.time);
        }
        public Test(String field){
                this();
            Test instance = new Test();
            this.field = field;
        }
        public static void main(String[] args){
            System.out.println("start...");
            Test t1 = new Test();
            System.out.println("-------");
            Test t2 = new Test("field1");
        }
    }
    
    0 讨论(0)
  • 2021-02-19 23:03

    In Java, the default constructor is the no-argument constructor that's implicitly provided by the compiler. And the compiler won't provide one in case you introduce any constructor with arguments.

    In that case you have to explicitly define a no-argument constructor (which is not default by the way, because it's not provided by the compiler), e.g. public MyClass() { }.

    And you can call it from other constructor as this();, which must be the first statement in the constructor where it's being called.

    0 讨论(0)
  • 2021-02-19 23:11

    For Java: You might mean the constructor without parameters. If so you can use the following code:

    public class MyClass {
       // no params constructor 
       public MyClass() {
          ...
       }
    
       // parametrized constructor
       public MyClass(int p1, String p2) {
           this();
       }
    }
    

    Hope this helps

    0 讨论(0)
  • 2021-02-19 23:11

    yes you can

    public YourClass() {
        public YourClass() { super();}
        public YourClass(int x) { this();}
    }
    

    provided you have the same argument constructor. This won't work

    public YourClass() {
        public YourClass(int x, int y) { this(); } // compiler error here
        public YourClass(int x) { super(); }
    }
    

    Note: super() calls the super constructor (in this case, class Object, because MyClass extends Object implicitly and class Object has a no arg constructor) that matches the same number of arguments.

    this() calls the constructor of the current class that matches the same number of arguments.

    0 讨论(0)
提交回复
热议问题