ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

How to Write Program and Class in Java Using Netbeans: Easy Steps

Updated on October 31, 2013

Get A Website Plus a Free Domain Name in Just 1 Hour!

Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!

This is a step by step process on how to program in Java. First, you must have a Java compiler or Java IDE, the word IDE stands for Integrated Development Environment, Java IDE is use to make simple Java projects. There are many Java IDE’s, as for myself, I use Netbeans IDE for my Java programming projects, and in this tutorial I will also use Netbeans IDE as an example on how to program in Java. Either you have Netbeans or other Java IDE’s it does not really matter, what matters is you must have an idea on how to program in Java in order to make some Java simple projects on whatever Java IDE you have. If you do not have an IDE you can download a free Java IDE or better download Netbeans IDE on the web, just look for it; if you want a safe free download you may browse it at cnet.com, just type cnet: Netbeans IDE on the google search box.

Updated: See how to install and where to download Netbeans IDE for Java Programming here!

How to Program in Java: Complete Simple Easy Steps

Assuming that you now have an installed Netbeans IDE.

Step 1: Open your Netbeans IDE

Step 2: Click FILE located at the left upper side, near the edge, and click the NEW PROJECT.

Netbeans IDE Start-up Page
Netbeans IDE Start-up Page

Step 3: Click JAVA at the categories column and click JAVA APPLICATION on the projects column, then click NEXT button below the dialog box. See sample snapshot below.

Netbeans IDE snapshot 2
Netbeans IDE snapshot 2

Step 4: Change the name of the PROJECT NAME in the dialog box to whatever names you want that will best describe your ‘will be’ java program, as for this tutorial, I will name the PROJECT NAME ‘javaSampleCoding’. After you finish typing the projects name, click FINISH button below the dialog box. See sample snapshot below.

Netbeans IDE Snapshot 3
Netbeans IDE Snapshot 3

Step 5: After clicking FINISH, a tab would open which has an embedded name ‘Main.java’. The interface should be like this.

Netbeans IDE Snapshot 4
Netbeans IDE Snapshot 4

On this Main.java tab, you can now code your first program. You can copy and paste my code on just click this link in a new tab. There are Java simple codes written there especially made for beginners, But before you proceed, read the NOTE below.

Note: Before you copy and paste the code on the link, make sure that you erase all the codes on the Main.java except for the codes in lined with package, as it would duplicate some of the codes if you don’t. The codes on my link above are already complete. You must try first the code that outputs: “This is just an example…” before the second given code. It is just actually like a hello world program. After copying and pasting, your interface should look like this.

Netbeans IDE snapshot 5
Netbeans IDE snapshot 5

Step 6: Run it by pressing CTRL + F6 for keyboard shortcut or click RUN -> RUN MAIN PROJECT on the toolbar.

After clicking the RUN, wait a moment. You can see that the program is compiling by looking at the lower left side of the IDE. Then,finally, after a few seconds you can now see the output of the program. It will look like this.

Sample Output 1
Sample Output 1

If you intend to make a new project, just repeat the steps above. You can experiment on System.out.print(); by changing the text inside it then run again the program to see the word you typed.

To further deepen your experience with Java programming and with Netbeans IDE, you should know how to make a Java class. Almost all of my sample Java source codes are in OOP (Object Oriented Programming), meaning the program is chopped in classes and methods. The codes are divided and is not all placed at the Main.java. The purpose of programming in this way is for the programmers to easily debug the program. Meaning, if there is something wrong with the program the programmers can easily detect it because the codes are chopped in groups.

Furthermore, to make a program that contains Java class, just repeat the STEP 1 – STEP 5, then proceed to STEP 7 below. But before doing this read first the Note below.

Note: As an example for java program which contains class, I will pick the sample java source code for the RECURSIVE DIAMOND SHAPE,This is the link for it. You can name your PROJECT NAME: ‘recursiveDiamondShape’.

Moreover, to identify the codes for java class and the code for main class, find this sign ->//java class, and all the codes below it are java class codes and stop copying if you see this sign-> //main class, because the preceding codes below it is for Main.java.

Now ready? After completing the STEPS 1-5, proceed to next step below.

STEP 7: Bring your cursor to PROJECTS, located at the left upper side just below the tool bar. Click it, then find your PROJECT NAME which is ‘recursiveDiamondShape’ click it, you will see SOURCE PACKAGES folder, then below it is the folder which name ‘recurseiveDiamondShape’, your program’s name. RIGHT CLICK it, then NEW-> then JAVA CLASS -> then name the CLASS NAME this: ‘Diamond’ then click FINISH button below, after that, a new tab will open at your Netbeans IDE which has a name Diamond.java.

For your Diamond.java, copy and paste the //java class codes on the link below. The interface for the Diamond.java, after the copy and paste, should look like this.

Diamond.java half body snapshot
Diamond.java half body snapshot

For your Main.java, make sure to erase all except for the code in line with package. The interface for the Main.java should look like this.

Main.java full body snapshot
Main.java full body snapshot

Then run the program by CTRL + F6 while you are on the Main.java interface, the sample snapshot for the output looks like this.

Program's output full body snapshot
Program's output full body snapshot

The program's output look like the snapshot above if the entered number is 12, to test the program again, just click Run, or CTRL + F6 and enter the number you want then hit ENTER on the keyboard.

Is this hub clear to you?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • Hakim Kimo profile image

      Hakim Kimo 5 months ago

    • profile image

      Shehu 22 months ago

      hy aisha91....please which book can you recommand to me when i want to lean java coding on netbeans thanks.

    • aisha91 profile image
      Author

      Rasna Aisha 4 years ago from Manila, Philippines

      Hi Subrata,

      Welcome. Glad to know it helps you. Thanks for your input and making us know. Happy Programming!

    • profile image

      Subrata 4 years ago

      Thanks ! I have been long stuck in writing my own class programs in net beans. Today it is solved. Thanks once again.

    • aisha91 profile image
      Author

      Rasna Aisha 5 years ago from Manila, Philippines

      Thanks :) Java and Java scripts are two different languages, but I like java script too. Glad you love it :)

    • His princesz profile image

      His princesz 5 years ago

      I forgot about Java scripts, used it 4 years ago. I love your systematic explanation and it's coming back to me again. Great!:)