Java Programming - Lesson 3

How to Create Your First Java Program Using NetBeans IDE

A Step-by-Step Guide for Absolute Beginners

Introduction

Welcome to another exciting lesson in our Java Programming series!

In our previous lesson, we explored how to download and install NetBeans IDE, a development environment that provides programmers with the tools needed to create Java applications.

Now that we have our development environment ready, it's time to put it into practice.

In this tutorial, we'll learn how to create our first Java project, understand the basic structure of a Java program, write a simple instruction, and execute it successfully.

By the end of this lesson, you should be able to create a basic Java application that displays a message on your computer screen.

Our first program will be the traditional Hello World application.

Let's get started!

1. What Is a Java Program?

A Java program is a collection of instructions written using the Java programming language to perform specific tasks.

These instructions tell the computer what operations to perform and how to perform them.

For example, a Java program can be written to:

Display messages on a computer screen.

Perform mathematical calculations.

Store and process information.

Manage business records.

Develop web applications.

Create desktop software.

Support mobile and enterprise applications.

Before developing complex applications, programmers usually begin with simple programs that help them understand the fundamental structure of the language.

The Hello World program is one of the most common examples used to introduce beginners to programming.

2. Launching NetBeans IDE

The first step is to open NetBeans IDE on your computer.

After the application has loaded, locate the New Project button.

It is usually represented by an icon containing a plus sign.

Click the button to open the New Project wizard.

The wizard will guide you through the process of creating a new Java project.

3. Creating a New Java Project

In the New Project window, select the appropriate project category and project type.

For this exercise, choose:

Category: Java

Project Type: Java Application

After making your selections, click Next.

NetBeans will display another window where you can enter the project details.

Choosing a Project Name

A project name identifies your application within the development environment.

Good programming practices encourage the use of meaningful and descriptive names.

For our first application, we'll use:

HelloWorld

It is generally advisable to avoid spaces in Java project and package names. Using HelloWorld instead of Hello World is a suitable convention.

Choosing a Package and Main Class

NetBeans may automatically generate a package name and a main class name based on the project name.

For this exercise, you can use:

Package name: helloworld

Main class name: HelloWorld

These names serve different purposes:

The package helps organise related Java classes.

The class identifies the particular Java component containing our program.

After confirming the project settings, click Finish.

NetBeans will automatically create the project and generate the required source files.

4. Understanding the Structure of a Java Project

After creating your project, NetBeans will display its structure in the Projects window.

A typical Java project may contain:

Project configuration files.

Source Packages.

Java source files.

Libraries and dependencies.

Build and output directories.

The most important component for this exercise is the Java source file.

You should see a file named:

HelloWorld.java

The .java extension identifies it as a Java source-code file.

Double-click the file to open it in the NetBeans editor.

This is where we will write our Java instructions.

5. Understanding Java Packages

At the beginning of the source file, you may see a statement similar to:

package helloworld;


What does this statement mean?

A package is a mechanism Java uses to organise related classes and interfaces.

Think of a package as a folder or organisational category that groups related programming components.

For example, a large business application might contain packages for:

Customer management.

Financial transactions.

Employee records.

Inventory management.

Reporting services.

Packages also help prevent naming conflicts.

Two different packages can contain classes with the same simple name without necessarily creating a naming conflict.

For example:

school.Student

and

university.Student

Both classes are named Student, but they belong to different packages.

Important: Java package names conventionally use lowercase letters. This is why helloworld is preferable to HelloWorld as a package name.

6. Understanding Java Classes

Next, examine the following declaration:

public class HelloWorld {


This statement declares a class called HelloWorld.

A class is one of the fundamental building blocks of Java's object-oriented programming system.

It provides a structure for defining objects, their data, and the operations they can perform.

Let's understand the components.

public: This is an access modifier that makes the class accessible from other parts of the application, subject to Java's access rules.

class: This keyword tells Java that we are declaring a class.

HelloWorld: This is the name of our class.

{ : This opening curly brace marks the beginning of the class body.

The corresponding closing curly brace marks its end.

In Java, curly braces are used to define blocks of code, including classes and methods.

7. Understanding the Main Method

Inside our class, we will find an important declaration:

public static void main(String[] args) {


This is the conventional entry point of a standard Java application launched using the traditional Java application launcher.

Let's examine its components.

A. public

The public keyword is an access modifier.

It allows the method to be accessed by the Java launcher.

B. static

The static keyword means the method belongs to the class rather than requiring an instance of the class to be created first.

This allows the application to start executing the main method without first creating an object of the class.

C. void

The void keyword indicates that the method does not return a value.

Some Java methods return information after performing their operations. The main method in this example does not.

D. main

The word main is the method's name.

It is recognised by the traditional Java application launcher as the starting point for executing the program.

Java is case-sensitive, so main, Main, and MAIN are treated as different identifiers.

E. String[] args

This represents an array of strings used to receive command-line arguments passed to the application when it starts.

An array is a data structure that stores multiple values of the same declared type.

The name args is conventional, but it can be replaced with another valid parameter name.

8. Understanding Comments in Java

Comments are notes that programmers add to their source code to explain particular instructions or sections of a program.

Java supports three commonly used comment styles.

Single-line comments

These begin with two forward slashes:

// This is a single-line comment


Everything following the two forward slashes on that line is ignored by the compiler.

Multiline comments

These begin with /* and end with */.

/*

This is a multiline comment.

It can contain several lines

of explanatory information.

*/


Documentation comments

Java also supports documentation comments, which begin with /** and end with */.

These are commonly used to document classes, methods, and other programming components.

Comments help make source code easier to understand, maintain, and share with other developers.

9. Writing Your First Java Instruction

Now comes the exciting part!

Inside the main method, enter the following Java statement:

System.out.println("Hello World!");


This is an instruction that tells Java to display the text Hello World! in the output console.

Let's break it down.

System

This refers to a built-in Java class that provides access to useful system-related facilities.

out

This represents the standard output stream.

It is commonly used to display information in the console.

println()

This is a method that prints the supplied information and then moves the output position to a new line.

"Hello World!"

This is the text we want to display.

The double quotation marks indicate that it is a string literal.

Semicolon (;)

Java uses semicolons to terminate most statements.

Therefore, our instruction must end with a semicolon.

10. The Complete Java Program

Now that we've examined the individual components, let's put everything together.

Here is the complete source code for our first Java application:

package helloworld;


public class HelloWorld {


    public static void main(String[] args) {


        System.out.println("Hello World!");


    }

}


You can copy this code into your NetBeans editor, provided your project uses the same package and class names.

Notice that the program contains:

A package declaration.

A class declaration.

A main method.

A print instruction.

These components form the basic structure of our introductory Java application.

11. Saving and Running Your Java Program

After entering the code, save your source file.

You can do this by clicking the Save icon in NetBeans.

Next, locate the green Run button, which may be labelled Run Main Project.

Click it to execute your application.

NetBeans will compile the source code and attempt to run the resulting application.

If everything is correct, the output console should display:

Hello World!


You should also see a message indicating that the build was successful.

Congratulations! You have successfully written and executed your first Java program.

12. What If Your Program Does Not Run?

Don't panic if your first attempt produces an error.

Programming involves identifying and correcting mistakes.

Here are a few common problems to check.

Missing semicolon: Ensure that your print statement ends with a semicolon.

Incorrect spelling: Java is case-sensitive. Write System.out.println correctly.

Mismatched braces: Check that every opening curly brace has a corresponding closing curly brace.

Incorrect class name: Ensure that your public class name matches the source filename.

JDK configuration: Make sure a compatible JDK is installed and configured in NetBeans.

Correcting these mistakes will help you develop good programming habits.

13. Practice Exercise

Now that you've created your first Java program, try this simple exercise.

Change the message:

System.out.println("Hello World!");


to:

System.out.println("Welcome to Java Programming!");


Save and run the program again.

Your console should display:

Welcome to Java Programming!


You can also try displaying your name:

System.out.println("My name is Emmanuel.");


Experiment with different messages to become more comfortable writing and executing Java instructions.

Conclusion

Congratulations on completing your first practical Java programming exercise!

You've learned how to create a Java project in NetBeans, understand the basic structure of a Java source file, recognise packages and classes, examine the main method, and display information using System.out.println().

Although this is a simple application, it introduces concepts that will remain important throughout your programming journey.

Remember, becoming a good programmer is a gradual process. Start with the fundamentals, practise regularly, learn from your mistakes, and continue challenging yourself with new projects.

Every great software application begins with an idea and a few lines of code. Yours has just begun!

Watch the Video Tutorial

Follow the accompanying video lesson to see how to create and execute your first Java application using NetBeans IDE.

Video lesson: Creating Your First Java Program:

https://youtu.be/V1KLEY_q5_I?si=a6ToEaGrgZCwshHn 

About Creative Skills & Hobbies Hub

Creative Skills & Hobbies Hub (CSHH) promotes creativity, know

ledge, practical skills, education, and personal development.

Our mission is to help people discover their potential, develop valuable abilities, and transform knowledge into opportunities.

Keep learning. Keep creating. Keep growing!

Comments

Popular Posts