Home How to reinstantaite Object arrays in implemented class of Interfaces?
Reply: 1

How to reinstantaite Object arrays in implemented class of Interfaces?

Athul Muralidharan
Athul Muralidharan Published in 2018-02-14 04:45:37Z

I have a Interface I which has a static function returnbjectsCreated()

Initially class A and B implemented I

But I want to keep track of the objects created.

So I created a class C that implements I ; A and B extends C I thought of appending the array in defObj[] in the constructor of C but didn't work as it was final.

    public interface I
        DefObj[] defObj;

     public static DefObj[] returnCreatedObjArr()
    return defObj;

Each time I create a new object for A or B, it needs to be appended to defObj in the interface.

i.e when I access I.returnCreatedObjArr() it should return all objects created.

What should I do ?

Note: No external packages or java collections can be used

Can you please add a snippet of that? The trouble I am facing right now is as follows :

private Node[] updateDefObj()
        DefObj[] new DefObj = new DefObj[defObj.length + 1];
        System.arraycopy(defObj.length, 0, new defObj.length, 0, defObj.length);
        defObj.[defObj.length+1] = this;
        return defObj.length;

class constructor:
     C() {

        defObj = new DefObj.length[defObj.length + 1] ;
        defObj = updateDefObj(defObj);  }
Amit Bera
Amit Bera Reply to 2018-02-14 06:08:54Z

You can create a wrapper class of Array (for my example MyInterfaceObjectArray) which will increment its size on the runtime. You can implement the increment logic as you wish, in my case, I have double the current size of Array also it starts from size 10. You can try like below:

public class Main {
    public static void main(String[] args) throws ParseException {
        MyInterfaceImpl impl;
        for (int i = 0; i < 25; i++) {
            new MyInterfaceImpl();
        System.out.println("Object Array : " + Arrays.toString(MyInterface.getObjects()));

class MyInterfaceObjectArray {
    MyInterface[] objects;
    private int offset = 0;

    MyInterfaceObjectArray() {
        objects = new MyInterface[10];

    public void addObjectToArray(MyInterface i) {
        if (offset == objects.length) {
            MyInterface[] copyObjects = new MyInterface[offset * 2];
            System.arraycopy(objects, 0, copyObjects, 0, offset - 1);
            objects = copyObjects;
        objects[offset] = i;

    public MyInterface[] getObjects() {
        return objects;


interface MyInterface {
    MyInterfaceObjectArray interfaceObjectArray = new MyInterfaceObjectArray();

    static MyInterface[] getObjects() {
        return interfaceObjectArray.getObjects();

class MyInterfaceImpl implements MyInterface {
    MyInterfaceImpl() {
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323071 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO