Create an Object from its own Class Tutorial

In Java, it is quite common to create an object out of its own class. As you know by now, the main method is the entry point for the class being invoked by the java command line tool. Future programs that you write will grow quite large over time and in the growth process your main method will become spaghetti. In this tutorial I will teach you how to create an object from its own class and also how to keep your main method clean and tidy.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md OwnClass
C:\Java>cd OwnClass
C:\Java\OwnClass>Notepad Box.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Box {
    // create a method to return the volume of the box
    int calculateVolume (int boxLength, int boxHeight, int boxWidth) {
        int returnValue; // local variable
        returnValue = boxLength * boxHeight * boxWidth;
        return returnValue;
    } 
}

Now switch back to the command prompt (CMD) and type in javac Box.java and press Enter.
Switch back to the CMD prompt and type in the following command.


C:\Java\OwnClass>javac Box.java
C:\Java\OwnClass>Notepad OwnClass.java


Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class OwnClass {
    public static void main(String args[]) {	
        
        // declare reference variable (ownClassReference) and allocate a new OwnClass object 
        OwnClass ownClassReference = new OwnClass();
	
        ownClassReference.displayGreeting();
	
        int volumeArray[] = new int[3]; // This will go away
        volumeArray = ownClassReference.getBoxVolumes(); // This will go away

        ownClassReference.displayVolumes(volumeArray); // This will go away

        //ownClassReference.displayVolumes(ownClassReference.getBoxVolumes());
    
        ownClassReference.displayGoodbye();
    }

    void displayGreeting() {
        System.out.println("Welcome, this program will calculate the volume of three boxes.");
    }

    int[] getBoxVolumes() {	
        Box box1 = new Box(); // declare reference variable and allocate new Box object
        Box box2 = new Box(); 
        Box box3 = new Box();
		
        int returnArray[] = new int[3];
        returnArray[0] = box1.calculateVolume(5, 7, 3); // invoke the calculateVolume method
        returnArray[1] = box2.calculateVolume(10, 5, 4); 
        returnArray[2] = box3.calculateVolume(15, 4, 10); 
		
        return returnArray;
    }

    void displayVolumes(int[] arrayOfVolumes) {
        int counter = 1;
        System.out.println();
        for( int tempVar : arrayOfVolumes ) {				
            System.out.println("The volume of box #" + counter + " is: " + tempVar);
            counter++;
        }
        System.out.println();
    }

    void displayGoodbye() {
        System.out.println("Thank you for using the Acme volume calculation program!");
    }
}

Now switch back to the command prompt (CMD) and type in javac OwnClass.java and press Enter.
Now type in java OwnClass and press Enter.


C:\Java\OwnClass>javac OwnClass.java
C:\Java\OwnClass>java OwnClass
Welcome, this program will calculate the volume of three boxes.
The volume of box #1: 105
The volume of box #2: 200
The volume of box #3: 600

Thank you for using the Acme volume calculation program!

Please watch the video for additional content.


Final thoughts

It is very common to create an object from its own class; it is really no different than creating an object of any other class. Once you create the object, you can access that class's members which in the case of this tutorial are the other methods. I kept the main method clean by calling methods instead of executing a bunch of statements. This tutorial utilized several concepts from the following prior tutorials:
Enhanced For Statement Tutorial
Introduction to Methods Tutorial
Single Dimensional Array Tutorial
Class Introduction Tutorial


Tutorials