Can I call a constructor from another constructor?

Asked 2 years ago, Updated 2 years ago, 119 views

Is it possible for a constructor to call a constructor within the same class? Not the lower class. What do I do?

java constructor

2022-09-22 22:37

1 Answers

It's possible!

    public class Foo
    {
        private int x;

        public Foo()
                {
                    this(1);
                }

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

This is how you can call a constructor in the same class Also, the creator of the higher class can use super to call it You can write on the first line of the constructor.


2022-09-22 22:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.