Create a Java Program – How to Create the simplest Java Program

In this article, we will see a video that explains how to create a Java program, and a very simple one, if not the simplest you can create. Even though this Java program is composed of only about seven or eight lines of code is a complete runnable program that creates an output to the console. First, let us take a look at a video, and then we will explain in plain text all the key concepts around how to create a Java program. You can read the article below and then watch the video or if you prefer to watch the video and then read the rest of the article. The article will explain the concepts shown but not discussed in the video.

 

The JavaClass1 program source code

package pk1;

public class JavaClass1 {

public static void main(String[] arg) {

System.out.println(“Hello Java Programmer, this is awesome!”);

}

}

Executing the Eclipse IDE

 

In the video we lauch the Eclipse IDE and we see the Eclipse logo shown in the above image.  Eclipse is an Integrated Develoment Environment.  An IDE can be though of as kind of a text editor on steroids.  In Eclipse you write your Java code in the Eclipse editor and compile, debug, test and document your program.

The Eclipse Workspace explained

 

When you start Eclipse execution at one point in time Eclipse is going to show a screen similar to the image above.  In here Eclipse is asking the user to select a workspace.  An Eclipse workspace is a folder in the computer’s hard disk that will contain the source code.  In the above image you see that the user can choose the location and the name of the workspace.  If the user writes in the dialog a name that does not exist, then a new folder with the given name and location will be created to be the workspace.  The user has the choice to use different workspaces, in other words the user can be working on a particular workspace, then he can change the workspace name and location and start working on a different workspace.  The user can be working on a workspace called work1, then he decides to copy some code that is in another workspace called work2, from the same Eclipse session the user can change the current workspace to work2, go do a copy then change the current workspace to work1 and paste the code in a file in work1.

A Java Project in Eclipse

 

A workspace can contain projects, as a matter of fact, it can contain one project only, or many projects.  In terms of definition a project is a subfolder inside the workspace folder.  The project will also contain many subfolders of his own.

The Source Code folder in Eclipse

 

The source code folder “src” is the subfolder inside the project folder that will contain the source code written by the Java developer.  Normally the src folder is going to contain more subfolders called packages.  The package concept is a way to avoid what is called name space clashes.  For instance if a Java developer is writting code for the accounting department and also the sales department.  There can be a java object called customer in the accounting department software, but the sales department also has its own customer object.  Both customer objects contain each its own set of attributes.  To illustrate the package concept in action in this scenario then, the complete name of the accounting department customer object will be abc.company.accounting.customer, and the sales object name will be abc.company.sales.customer, therefore the accounting department package is “abc.company.accounting”, and the sales department package is “abc.company.sales”.  You can think of packages kind of like first names combined with last names.

Creating a Package in Eclipse

The above dialog is presented when the developer wants to create a package.  The dialog contains the project name and the source folder in the first field of the dialog and the package name in the second field.  In this case the package name is pk1 and the path to the package inside the project is JavaProject1/src/pk1.

 

In the image above you can see the folder structure of the project inside the workspace. As you can see in the workspace beside the JavaProject1 project, there are also other Java projects such as @001_MultiThreading, JavaSe0812v1, JavaSe0812v2, JavaSe0814v1, JavaSe0814v2, etc. Each one of those folders is a Java project in and of itself. Notice the structure of JavaProject1 that contains subfolders called src, and pk1.

Creating a class in Eclipse

 

In Eclipse you can create Java classes. Above is a dialog used in Eclipse to create a class. Classes are java files inside of packages. The classes are the files that contain the actual source code. Inside a class, you have to write methods and fields. The methods imply action, the fields are where the data is stored.

 

Above we have a class with no methods and no fields, just the class definition. A class like the one above does nothing. When you add methods to the class then the class does the actions the methods defined.

What is a Method in a class

The class JavaClass1 above is declared as a public class. The public keyword is used to declare that any other class can access members inside the class. This class above contains a method. The method name is main. It is a public method, this means that this method is visible to all other classes.

The Eclipse Java Console

 

The Java Console is where all output generated by the System.out or System.err lands. In the class JavaClass1 we have a statement that reads:

System.out.println(“Hello Java Programmer, this is awesome!”);

This statement has a message in between double quotes, so when the program executes this statement the output which is all the text in between double quotes will be directed to the console.

Conclusion

In this article, we presented a video that explains how to create a Java program. In the video, we see the process of the creation and all the windows and screens you need to use to enter information in Eclipse. We saw how to launch an Eclipse session, how to select, use an existing one, or create a new workspace to contain our projects. We talk about what is a project and components of a project such as a source code folder, packages, and classes. We mention class members such as methods and fields. And the last thing we talked about was the Java Console.

Leave a Comment