In How To Write Your First Java Program, we installed an IDE and wrote a basic Java program. We also learned about the essential components of that program’s Java source codes – the keyword class and the main method. Here on after, we’ll use the word “application” to refer to the word “program.”
Java Classes
What are Java classes? Java classes are blueprints and building blocks akin to atoms. These are codes we write using the class keyword. As blueprints, they represent the design of the whole application. Like building blocks, we can build them into bigger chunks and, finally, the application itself. These two metaphors are meaningful because Java generally deals with classes. In other words, everything in Java is a class (except for a few minor things we’ll talk about in later posts).
Java Objects
Java objects are live instances of Java classes. When we create something based on a blueprint, we create a living entity of that blueprint. In Java speak, we call that entity an object. If we had a design for a car, that is its blueprint. On the other hand, when we built an actual vehicle based on that design, that car is an object.
If we only had a class for our application, how do we create an object from that class? If we’re starting up from that same class, we can’t. Only the JVM does that.
IMPORTANT
When we talk about creating something from a blueprint, we are expressing that intent in the codes. The actual object creation happens when the application is running inside the JVM. Therefore, Java objects are live instances within a running application.
We have these classes – ToyotaCarApplication (startup class) and ToyotaFortunerBlueprint (blueprint class).
1 2 3 4 | public class ToyotaFortunerBlueprint { } |
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class ToyotaCarApplication { public static void main(String[] args) { // Mother's actual Toyota Fortuner car ToyotaFortunerBlueprint motherToyotaCar = new ToyotaFortunerBlueprint(); // Father's actual Toyota Fortuner car ToyotaFortunerBlueprint fatherToyotaCar = new ToyotaFortunerBlueprint(); // Elder son's actual Toyota Fortuner car ToyotaFortunerBlueprint son1ToyotaCar = new ToyotaFortunerBlueprint(); // Younger son's actual Toyota Fortuner car ToyotaFortunerBlueprint son2ToyotaCar = new ToyotaFortunerBlueprint(); } } |
Lines 8, 11, 14, and 17 express the intent of creating four unique live instances of Toyota Fortuner based on the blueprint class ToyotaFortunerBlueprint. When the JVM executes these lines, it will create actual live objects whose lives end when the application completes its execution and terminates.
What’s Next?
That was an easy ride! Next, we’ll look at Java objects and their properties and behaviors.