Lesson 2
Introduction
In this tutorial i will be explaining the basics of netbeans and its directory structure which it creates when we compile java applications. We will also be briefly touching on some concepts about java that you need to know. So first open up netbeans. Once its opened were going to make a new project. We can do this by pressing either the new project button which is orange in colour and found under the Edit tab at the top left of the window. Or you can press File then select new project. Once the new window opens we can select from the left column Java, and from the right window Java Application. Press next to move on, we now need to give the project a name and a location, i would recommend something memorable and a location such as my documents or your desktop. You will want to un-check the bottom check-box which says Create Main Class we don't need this as i will be explaining this at a later date. Then hit finish to make up your first project folder.
once your window has closed back to the main window you will see you project has been created in the column on the left with the name you gave it. You can see it is in bold. if you have multiple projects you main one will be in bold so when you press compile that is the one that will be used. Next to your project name you will see a little box with a + in a square box if you click that it will show all the files inside your project. In the source files folder you wont see anything as we haven't made any yet. In the other folder called libraries will be the JDK you installed earlier, this contains all the various different files java needs that have already been made by the creators of java themselves.
So to begin you want to click on the default package which is in grey and select other near the bottom of the menu. A new window will open. From the categories column select java and in the right File Type column select Java Main Class. This will hold the main method which i will explain in just a moment. Hit next to go to the next page where you need to name your Main class. I would name it Tutorial. Note it is important to use good java conventions whilst naming classes. What i mean by this is to Start each word with a Capital letter and no spaces between so 'Tutorial' or 'ThisIsMyTutorial' you can have letter, number or underscore in the name but no spaces and the classes cannoy begin with a number; letters or underscores to start is fine.
Now if you open up your default package on the left you will see that your main class has been created for you and if you click on it you will see it open on the right side in the main window. Now you should abit of generated code which netbeans has kindly already pre-made for you, you can highlight all this and delete it.
So now i am quickly going to go over the files that netbeans will generate for you when you create a new package, project, classes etc. If you go to the place where you created your project folder for example i saved mine on my desktop for easy access you will see in the main folder 3 other folders and 2 files. Now these hold all the different folders that your project needs to run. If you open up the src folder you will see the java main class we just created. So if you ever need to find your source files in the folder outside of netbeans you now know where they are.
So now we shall begin to learn some java! the very first thing you should now is that all our code goes inside of classes. A simple analogy of this would be to look at a class as a folder. All the code must be stored within this folder so that it knows where to find it. Well how do we make a class. Type the word class into the main window and you will see it changes to a blue colour this is because class is a keyword in java , next we need to give it a name this is always the thing java will look for after you type class. A very important thing to know right now is that the name you give must be the same EXACTLY to the file name you gave your class. So from above it would be ' class Tutorial ' note the capital T. These rules regarding class names will change slightly as we learn more but for now just remember it must be the same as the file name. You will see that we have a red circle next to what we have typed so far and that's because we need to open and close our class. We do this by using pointy brackets: { When you see them being used it will be like this:
class Tutorial {}
When you hover over one of them in in netbeans it will highlight its partner if there is one. So now we have made a main class and named it Tutorial and we have opened and closed brackets. We refer to the area between brackets as inside of a certain area or method. Next we need to give the program a starting point else it could start anywhere. This starting point is represented by something called the main method. You might recognize it from the last tutorial and we will go over it in more detail very soon. Every single main class will have a main method with a few exceptions, it will look something like this:
class Tutorial { public static void main(String [] args) { }}
now when you look at the line of code i added you may think it looks really confusing and you want to quit java and thats that. Well don't it really isn't that hard at all. All's you need to know for now is that the code inside the public static void main is the code that will be executed first. Now were just going to make the most basic program possible by printing something out to the screen. Go ahead and type out the code below by yourself and make sure everything is in the right place. It's very important you learn to type it out yourself and not copy and paste.
class Tutorial { public static void main(String [] args) { System.out.println("Hello World!"); }}
by now i hope you have had a go at typing out the code in the box above. Lets just take a look at some of the key things you should of put in that a lot of people miss out. Make sure there is a capital S for System, make sure to use a semi colon ; after the closing bracket. A short note on semi colons. Every line of code in java that is in between those spikey brackets should have a semi colon at the end of it ( that means if your not declaring a class or method with spikey brackets ) Also make sure your using double quotes either side of Hello World. Most new java users will find remembering public static void main... and the rest of that line the hardest part. So now you have your first and very basic java program. You should be very proud of yourself!
For now you don't need to know how any of the above works. I would recommend that you memorize that second line public static void main into your head, everything about it. If you can crack that as soon as possible you will be all good. I will end the tutorial by getting you to run your first program and to do that you click on the green arrow shape at the top of the screen underneath profile and team tabs. You can also press on the keyboard Shirt+F6. To see the output click on the word output at the bottom left of your window.
That's it for now in the next tutorial tomorrow i will be teaching you about the basics of variables.
If you liked this post please Like it using the green button to the right of this sentence