Updated date:

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 2

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 1 | Lesson 3 >>

Lesson 2: Understanding Java Code Window

In our previous lesson, we saw what comprises of Java development environment and how we can install Java. In this lesson you will learn how to create your first Java project and how to interpret the NetBeans code editor window.

If you have not yet opened the Java NetBeans Integrated Development Environment (IDE) then double click the NetBeans icon on your desktop and open the Java Netbeans IDE.

You are ready now to start creating your first project. When you double click the NetBeans icon on your desktop, Java will open with a screen like the one shown above. Be patient, Java takes some time to open all the modules.

NetBeans learning resources

NetBeans learning resources

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-2

The initial window will present to you all the Java environment sections. As a beginner, spare some time to familiarize with the interface and go through the provided tutorials, sample projects and demos that provide valuable information.

You can access these resources by clicking Help >> Start Page on the menu bar at the top of the screen. To create a new project, click File >> New Project from the NetBeans menu bar and you'll get the following dialogue box.

New Java Project window

New Java Project window

Because we are creating a Java application, click Java under Categories and Java Application under Projects, then click the Next button to proceed to the next step.

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-2

In the Project Name part at the top, type a name for the Project. You will notice that the text at the bottom changes to match the project name in the Create Main Class text box. Now concentrate on the red bordered part above.

What we are doing here is that we are creating a Class called MyFirstProgram with capital “M”, capital “F”, capital “P” which is contained in a package called myfirstprogram, but with a lowercase "m", lowercase "f” and lower “p”.

The Project Location shows where your projects will be saved and in which Project Folder. You can change these to suit your own preferences.

Click the Finish button and NetBeans will create all the necessary files for your project. Clicking finish will take you back to NetBeans IDE.

Look at the Projects area at the top left of the screen (if you can't see it, click Window >> Projects on the menu bar).

Projects area

Projects area

Projects area

If you can’t see MyFirstProgram.java click the plus symbol on the Source Packages folder to expand your project. On the right hand side of the screen is the code editor window and it should display the same MyFirstProgram.java project. If you can't see the code editor window, double click MyFirstProgram.java in your Projects window above. The code will appear like shown below, with your name as the program author.

Code editor window

Code editor window

Code editor window

Notice in the code editor diagram above the class is called MyFirstProgram.java

public class MyFirstProgram.java {

This is the same name as the java source file in the project window MyFirstProgram.java. When you run the program the compiler demands that the source file and the class name should match.

So if the .java file is called myfirstprogram but the class is called MyFirstProgram then an error will occur in your program, however the package name can be different and it doesn’t have to be the same as the source file or the class name.

Java Comments

If you look on the code editor window, you'll notice that some text is greyed out, with forward slashes (/) and asterisks (*). The greyed out areas are called comments.

When the program is executed, comments are ignored by the compiler and so you can use the comments to document your program e.g. to explain what is happening at each stage of your code.

To use a single line comment use double forward slashes (//)

Example:

//Here is a single line comment.

To use multi-lines comments you can either use many single line comments

Example:

//Here is a multi-lines comment

//that makes use of two single line comments

Or you can use slashes (/) and asterisks (*)

Example:

/*

Here is a multi-lines comment

that makes use of slashes (/) and asterisks (*)

*/

Notice the above multi-line comment starts with /* and ends with */.

Sometimes we also use comments when we want to trace errors in the program. You can select a segment of a program code and then use the comment and uncomment toolbar shown below:

Comments toolbar

Comments toolbar

Comments toolbar

Have a look at the code editor window diagram again. The comments that starts with a single forward slash and two asterisks ( /** ) and ends with an asterisk and one forward slash ( */ ) are called Javadoc comments. Each text line of Javadoc comment starts with one asterisk.

Example:

/**

*This is a Javadoc comment

*Each line starts with one asterisk

*/

The purpose of Javadoc comments is to document your code. The documentation can then be converted into a HTML (Hyper Text Markup Language) document that can be helpful to your code users. To convert your code to HTML, click Run from the menu bar and select Generate Javadoc.

You might not see a useful HTML document since you have not added much code to your program yet. For now you can write your code with no or minimal comments so we are going to delete all the comments inserted by NetBeans but we shall add comments as we proceed.

Here is our code window without comments

Code window without comments

Code window without comments

Java is case sensitive, that means if a keyword is supposed to be typed in uppercase and then you type it in lowercase you'll get an error when compiling the code.

Also, note that Java code instructions ends with a semicolon (;). Missing on the proper case and omitting the semicolon are some of the common errors in Java programming by beginners so, watch out!

The structure of a Java class

Let us now examine the following segment of our code:

public class MyFirstProgram {

}

The above part of the code is called a class segment and it denotes the starting and ending of a Java class segment code. The start of a code segment is done with a left curly brace ({) and ends with a right curly brace (}). Any code inside the left and right curly braces belongs to that code segment.

Notice inside the curly braces for the class segment is another code segment shown below:

public static void main( String[ ] args ) {

}

The above part of the code is called a method, public denotes that this segment of the code can be accessed outside the class without creating any new objects as denoted by the keyword static. Void means this part of the code does not return any value.

The “main” part denotes the starting point of the Java program. When you run the program, Java compiler locate for this part of the code and execute any code inside “main”. Always include a “main” in your code.

Having understood the Java code window, we can now embark on writing our first Java program. We will do this in Lesson 3.

<< Lesson 1 | Lesson 3 >>


Related hubs...

Comments

Jeine on January 04, 2013:

This is awesome!