mikelplhts
mikelplhts

Reputation: 1211

Call a method after the constructor has ended

I need to call a method after the constructor has ended, and I have no idea what is the better approach.

I have this class:

class A {
    public A() {
        // ...
    }
        
    public void init() {
        // call after the constructor
    }
}

How do I call init() after the class A has been created?

Upvotes: 21

Views: 26050

Answers (6)

acsadam0404
acsadam0404

Reputation: 2831

If you want to call method BEFORE constructor you can use initializer block. https://www.geeksforgeeks.org/g-fact-26-the-initializer-block-in-java/

class A {
    { 
        init() 
    }

    public A() {
        //todo
    }

    private final void init() {
       //todo
    }
}

Upvotes: 1

UniversE
UniversE

Reputation: 2507

I pick up some ideas and provide an abstractable solution:

class A {
    protected A() {
        // ...
    }
    protected void init() {
        // ...
    }
    public static <T extends A> T create(Class<T> type) {
        try {
            T obj = type.newInstance();
            obj.init();
            return obj;
        } catch (ReflectiveOperationException ex) {
            System.err.println("No default constructor available.");
            assert false;
            return null;
        }
    }
}

Upvotes: 1

aioobe
aioobe

Reputation: 420951

You either have to do this on the client side, as so:

A a = new A();
a.init();

or you would have to do it in the end of the constructor:

class A {
    public A() {
        // ...
        init();
    }

    public final void init() {
        // ...
    }
}

The second way is not recommended however, unless you make the method private or final.


Another alternative may be to use a factory method:

class A {
    private A() {  // private to make sure one has to go through factory method
        // ...
    }
    public final void init() {
        // ...
    }
    public static A create() {
        A a = new A();
        a.init();
        return a;
    }
}

Related questions:

Upvotes: 21

David Conrad
David Conrad

Reputation: 16359

You will need a static factory method to construct the object, call the init method, and finally return the object:

class A {
    private A() {
        //...
    }

    private void init() {
        //Call after the constructor
    }

    public static A create() {
        A a = new A();
        a.init();
        return a;
    }
}

Notice I have made the constructor and the init() method private, so that they can only be accessed by the factory method. Client code would make objects by calling A.create() instead of calling the constructor.

Upvotes: 4

AngularLover
AngularLover

Reputation: 364

What did you so far? Are you looking something like this?

  Class A {
        public A() {
            //...
        }

        public void init() {
            //Call after the constructor
        }
    }

     public static void main(String[] args)
    {
    A a = new A();

    a.init();
}

Upvotes: 1

Nico
Nico

Reputation: 6893

Why not this :

Class A {
    public A() {
        //... Do you thing
        this.init();
    }

    public void init() {
        //Call after the constructor
    }
}

Upvotes: -2

Related Questions