ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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 

      18 months ago

    • profile image

      Shehu 

      2 years ago

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

    • aisha91 profile imageAUTHOR

      Rasna Aisha 

      5 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 

      5 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 imageAUTHOR

      Rasna Aisha 

      6 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 

      6 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!:)

    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)