How do I call one constructor from another in Java?

前端 未结 21 2510
不知归路
不知归路 2020-11-22 01:06

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if th

相关标签:
21条回答
  • 2020-11-22 01:22

    I know there are so many examples of this question but what I found I am putting here to share my Idea. there are two ways to chain constructor. In Same class you can use this keyword. in Inheritance, you need to use super keyword.

        import java.util.*;
        import java.lang.*;
    
        class Test
        {  
            public static void main(String args[])
            {
                Dog d = new Dog(); // Both Calling Same Constructor of Parent Class i.e. 0 args Constructor.
                Dog cs = new Dog("Bite"); // Both Calling Same Constructor of Parent Class i.e. 0 args Constructor.
    
                // You need to Explicitly tell the java compiler to use Argument constructor so you need to use "super" key word
                System.out.println("------------------------------");
                Cat c = new Cat();
                Cat caty = new Cat("10");
    
                System.out.println("------------------------------");
                // Self s = new Self();
                Self ss = new Self("self");
            }
        }
    
        class Animal
        {
            String i;
    
            public Animal()
            {
                i = "10";
                System.out.println("Animal Constructor :" +i);
            }
            public Animal(String h)
            {
                i = "20";
                System.out.println("Animal Constructor Habit :"+ i);
            }
        }
    
        class Dog extends Animal
        {
            public Dog()
            {
                System.out.println("Dog Constructor");
            }
            public Dog(String h)
            {
                System.out.println("Dog Constructor with habit");
            }
        }
    
        class Cat extends Animal
        {
            public Cat()
            {
                System.out.println("Cat Constructor");
            }
            public Cat(String i)
            {
                super(i); // Calling Super Class Paremetrize Constructor.
                System.out.println("Cat Constructor with habit");
            }
        }
    
        class Self
        {
            public Self()
            {
                System.out.println("Self Constructor");
            }
            public Self(String h)
            {
                this(); // Explicitly calling 0 args constructor. 
                System.out.println("Slef Constructor with value");
            }
        }
    
    0 讨论(0)
  • 2020-11-22 01:23

    Yes, any number of constructors can be present in a class and they can be called by another constructor using this() [Please do not confuse this() constructor call with this keyword]. this() or this(args) should be the first line in the constructor.

    Example:

    Class Test {
        Test() {
            this(10); // calls the constructor with integer args, Test(int a)
        }
        Test(int a) {
            this(10.5); // call the constructor with double arg, Test(double a)
        }
        Test(double a) {
            System.out.println("I am a double arg constructor");
        }
    }
    

    This is known as constructor overloading.
    Please note that for constructor, only overloading concept is applicable and not inheritance or overriding.

    0 讨论(0)
  • 2020-11-22 01:26

    Yes, it is possible:

    public class Foo {
        private int x;
    
        public Foo() {
            this(1);
        }
    
        public Foo(int x) {
            this.x = x;
        }
    }
    

    To chain to a particular superclass constructor instead of one in the same class, use super instead of this. Note that you can only chain to one constructor, and it has to be the first statement in your constructor body.

    See also this related question, which is about C# but where the same principles apply.

    0 讨论(0)
  • 2020-11-22 01:26

    You can a constructor from another constructor of same class by using "this" keyword. Example -

    class This1
    {
        This1()
        {
            this("Hello");
            System.out.println("Default constructor..");
        }
        This1(int a)
        {
            this();
            System.out.println("int as arg constructor.."); 
        }
        This1(String s)
        {
            System.out.println("string as arg constructor..");  
        }
    
        public static void main(String args[])
        {
            new This1(100);
        }
    }
    

    Output - string as arg constructor.. Default constructor.. int as arg constructor..

    0 讨论(0)
  • 2020-11-22 01:28

    Yes it is possible to call one constructor from another with use of this()

    class Example{
       private int a = 1;
       Example(){
            this(5); //here another constructor called based on constructor argument
            System.out.println("number a is "+a);   
       }
       Example(int b){
            System.out.println("number b is "+b);
       }
    
    0 讨论(0)
  • 2020-11-22 01:29

    Pretty simple

    public class SomeClass{
    
        private int number;
        private String someString;
    
        public SomeClass(){
            number = 0;
            someString = new String();
        }
    
        public SomeClass(int number){
            this(); //set the class to 0
            this.setNumber(number); 
        }
    
        public SomeClass(int number, String someString){
            this(number); //call public SomeClass( int number )
            this.setString(someString);
        }
    
        public void setNumber(int number){
            this.number = number;
        }
        public void setString(String someString){
            this.someString = someString;
        }
        //.... add some accessors
    }
    

    now here is some small extra credit:

    public SomeOtherClass extends SomeClass {
        public SomeOtherClass(int number, String someString){
             super(number, someString); //calls public SomeClass(int number, String someString)
        }
        //.... Some other code.
    }
    

    Hope this helps.

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