MatiasMerkuri
12/30/2018 - 12:30 AM

Object Holding Class

OBJECT HOLDING CLASS

class First{
    public static void main(String args[]){

      // Making an object for the Fifth Class so we can get the add() method.
        Fifth obj = new Fifth();
        
        // Making an object array with 5 values max
        Second array[] = new Second[5];
        
        // Assigning array indexes to objects of the Second class
        array[0] = new Second();
        array[1] = new Second();
        array[2] = new Second();
        array[3] = new Second();
        array[4] = new Second();

        // Making a for loop to loop through each object and call the method on them
        for (int x = 0; x < array.length; x++){
            obj.add(array[x]);
            
        }
    }
}
public class Fifth{

// Making an object holding array
    private Second[] theList = new Second[5];
    private int i = 0;

// Making method which takes a Second object as an argument
    public void add(Second d){
    
    // Making an if statement if the array is full
        if (i < theList.length){
        
        // If it returns false it would assign an object to that array element
            theList[i] = d;
            
            System.out.println("Added at index " + i);
            i++;
        }

    }

}