How to Instantiate an Object in Java
In Java programming, instantiating an object means to create an instance of a class. To instantiate an object in Java, follow these seven steps.
- Open your text editor and create a new file. Type in the following Java statements:
- Save your file as InstantiateAnObjectInJava.java.
- Create another new file in the same directory. Type in the following Java statements:
- Save your file as Person.java.
- Open a command prompt and navigate to the directory containing your new Java programs. Then type in the command to compile the Person class and hit Enter.
- Type in the command to compile your Java application and hit Enter.
- You are ready to test your Java program. Type in the command to run the Java runtime launcher and hit Enter. Notice the output of the program that displays the
fullNamevalue of your object.
The object you have instantiated is referred to as
The Person class serves as the template for the object your program will create at runtime.