Chapter 1 - Java for Beginners Course

Java Application Example - Running our first app

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:

  1. We are defining a new public class called AirplaneApplication

  2. The class has one static method called main (we’ll cover the static 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:

  1. public static void main(String a[])

  2. public static void main(String[] a)

  3. 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.

Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Main class for this step
Dependencies

This is a list of recommended tutorials or courses that might be useful before starting this one.

Contents
Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations