ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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

Updated on October 16, 2019
dwachira profile image

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 | Source
Source

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 | Source

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.

Source

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 | Source

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 | Source

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 | Source

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 | Source

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.

Comments

    0 of 8192 characters used
    Post Comment
    • profile image

      Jeine 

      6 years ago

      This is awesome!

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)