Now that we have defined our example Airplane
class and seen some examples of creating objects, let’s try to run this in Java.
We’ll be switching gears in this section and move more to the practical side and will come back to the theory in the next section.
Let’s create a separate Java file called AirplaneApplication.java
with the following code:
public class AirplaneApplication {
public static void main(String[] args) {
Airplane plane1 = new Airplane();
plane1.setManufacturer("Boeing");
plane1.setNumberOfSeats(270);
plane1.printDetails();
Airplane plane2 = plane1;
plane2.printDetails();
Airplane plane3 = new Airplane();
plane3.setManufacturer("Airbus");
plane3.setNumberOfSeats(300);
plane3.deployLandingGear();
plane3.land();
plane3.printDetails();
System.out.println("plane1 is pointing to the same instance as plane2: " + (plane1 == plane2));
System.out.println("plane1 is pointing to the same instance as plane3: " + (plane1 == plane3));
}
}
There are two aspects we can see from this example:
-
We are defining a new public class called
AirplaneApplication
-
The class has one
static
method calledmain
(we’ll cover thestatic
modifier later)
The main
method
In Java, the entry point of our application is defined via a main method that must have the same signature as in the example above, this is: public static void main(String[] arguments)
.
By entry point we mean the point where our application code starts and will be the first method that is called by the Java Virtual Machine when our program is executed.
This method signature must be as defined by the Java language, meaning that methods with different names or with different parameters won’t be accepted.
For completeness, the variations that are accepted are:
-
public static void main(String a[])
-
public static void main(String[] a)
-
public static void main(String… a)
Executing the application from command line
1. Compiling the classes
If you’ve completed the Tools - Basic Setup tutorial, in the Java section it introduced the Java Compiler binary (javac
). We will be doing the same set of steps but for the 2 classes we have created so far, Airplane
and AirplaneApplication
.
If you don’t have Java installed, please follow the Tools - Basic Setup tutorial before proceeding.
First of all, open a Terminal and go to the folder that contains the two classes we created. If you don’t have the files yet, feel free to create an empty folder and create the Airplane.java
file mentioned before and the AirplaneApplication.java
file mentioned at the top of this page.
Once in the Terminal, run:
javac Airplane.java AirplaneApplication.java
If everything goes well, the command shouldn’t produce any output to the Terminal, but instead you should end up with the 2 compiled class files, Airplane.class and AirplaneApplication.class in the same folder.
2. Executing the Java application
Now that we compiled our Java classes to bytecode in the previous step (the generated .class
files), we can run our AirplaneApplication
code. To do this, we make use of the java
executable.
In the Terminal, run the following command:
java AirplaneApplication
Output:
manufacturer=Boeing, numberOfSeats=270, landed=false, gearDeployed=false
manufacturer=Boeing, numberOfSeats=270, landed=false, gearDeployed=false
manufacturer=Airbus, numberOfSeats=300, landed=true, gearDeployed=true
plane1 is pointing to the same instance as plane2: true
plane1 is pointing to the same instance as plane3: false
Notice that in comparison to the javac
command, when executing the application (java AirplaneApplication
) we only specified one argument (AirplaneApplication
) as this is the class that contains the main method and is the class we want to use as our entry point.
Executing the application from your IDE
If you downloaded the code (from the Code in GitHub section to the right hand side of this page) and imported it into your IDE, or if you’re using your IDE with your own project, feel free to execute the code from inside the IDE.
In case you are uncertain about how to proceed inside the IDE, you can follow the example in the Tools - Basic Setup for IDE tutorial about how to execute the application from the IDE. Just make sure you use the AirplaneApplication class instead of the ExampleApplication that the other tutorial uses as the demo.
Going forward, we’ll assume you have an IDE and that you’re executing the code from inside the IDE.
We won’t be manually compiling/executing the Java demos from command line for the remainder of this course unless we are covering concepts directly related to additional options in these commands.