NetBeans (and IDEs in general) contains many features that help you write code and fix errors quickly. Much of this was covered in INFO201, but you will be a lot more efficient if you learn how to properly use these features, so it is worth going through again.
Create a simple Student
domain class that contains fields for name and ID:
Create the class by right clicking the <> package in the project pane and selecting <
Java Class">>. Name the classStudent
, and leave it in the lab01
package. Start by declaring the two fields.
Remember that a field (which is sometimes called an attribute, property, or data field) is declared at the class level (not inside a method). The declaration will consist of an access modifier (fields are usually private
), a type (String
, Integer
, etc.), and an identifier (which is a name that identifies what the field will contain — in this case name
or id
(remember the convention that anything that isn't a class name should start with a lowercase letter).
Generate the getters and setters (sometimes called accessors and mutators) using the <
Insert Code ">> menu. Most of the items in the source menu are also available in the right click pop-up menu in the source editor.Generate a constructor that initialises both fields using the insert code feature again (using the same <
Insert Code ">> menu).Declare another field called papers of type ArrayList
using the following line of code:
private List<String> papers = new ArrayList<>();
You will get an error stating that ArrayList
and List
can not be found (hover your cursor over the red squiggly lines to see the error message). This is because you need to import the java.util
package since that is where these classes are located.
NetBeans will do this for you if you use the <
Fix Imports">> menu item.Notice that there are multiple List
classes. You will run into this problem frequently — you need to be careful to select the correct version of the class to import otherwise you will get more compiler errors. If you get the wrong one then go up and delete the incorrect import
statement and try again.
Delete the import statement that NetBeans added for you so that you get the same error again.
Notice how there is a light bulb in the bar to the left of the error. This means that NetBeans thinks it knows how to fix this error for you.
Click the light bulb. Examine the options presented to you and choose the best option to solve the problem.
Again, you need to be careful with the fix code feature. Sometimes the suggestions that NetBeans makes do not make sense, or sometimes it has multiple suggestions. This feature is only there to save you from having to type so much — it is not there to save you from having to think about what you are doing and you can make a problem much worse if you always select the first option.
Delete the import
statement again and this time build your project using <
The errors are usually linked to the source code so you can click a link in the output pane to jump to the code that caused the error.
Fix the error again using one of the mechanisms that you have just been shown.
Use the following instructions to add a toString
method that returns the name and ID:
Click somewhere in the class (not inside a method) and hit < Insert">> . This is the shortcut for the insert code feature. Select <
>.Select which fields that you want to be returned by the toString method (ID and name).
Switch to the Main
that you created earlier that contains the main
method.
Add code to the main method that will create an instance of your Student
class and print out the result of calling the toString()
method.
If you have forgotten how to create an instance of a class, it looks like this:
// create a string instance String badger = new String("honey badger");
Obviously, this code is creating an instance of String
. Your code should create an instance of Student
.
Don’t forget about the sout
template for generating the code to print to the console.
Keep your code formatted nicely using < Shift > f">> or <
Format">>.You can comment out a chunk of code by selecting it and using < /">>. The same again will uncomment the code.
You might have noticed already that a menu pops up while you are typing in code sometimes. This feature is called code completion. Type System
then press <> . You should see the pop-up menu that will show you all of the members that the System
class has available.
You can bring this menu up anytime using < Space">>.
Type sout
and hit < Space">> to complete the template. Delete everything between the brackets (if there is anything). Hit < p">>. This should pop up a menu showing you all of the parameter options that are available for this method. This is useful if you can’t remember which parameters a method accepts, or which order the parameters occur.
When working on larger projects where you are working with lots of files you will start having trouble finding the file you are looking for. There are two more keyboard shortcuts that you will find useful:
< Tab">> and keep the <> key held down: This opens a file switcher that has the open files ordered by last access (the most recently accessed files will be at the top). You can easily navigate this pane using the arrow keys, so it is an effective mechanism to navigate your project without needing to reach for the mouse (which is a good habit to try to develop — you are much faster if you can stick to the keyboard alone).
You can also use < Tab">> to quickly toggle between two open files. This is useful if you find yourself switching between the same two files a lot which is quite a common occurrence.
< o">> : Allows you to search for a type (class/interface) by name which can be useful if your project contains a lot of files. Note that your INFO202 course project will contain a lot of files.
Take a quick look at <
Keyboard Shortcuts Card">> . This should open up a PDF containing the default keyboard shortcuts and templates. Try to learn the keyboard shortcuts for the features that we have used in this lab so far. You will be far more efficient at editing code if you can use the IDE to do most of the heavy lifting for you.Code that the IDE adds for you will generally be correct, conform to industry standards, and will not contain typos and spelling mistakes.