Source Code and Methods


Collapse Content

The previous nodes didn't involve changing actual code, you just created Objects from Classes and interacted with their methods. The actual Classes are created by editing their source code, which can be done in BlueJ by double-clicking on any class icon.

classes

This will open up the source code file:

source-code-student

Now you can see some of the code you interacted with before. You don't need to worry about every detail of code, but find the method getStudentID. We already covered how methods take in parameters, but now let's examine the beginning of a method header:

BlueJ-method-header

Access
public String getStudentID()

public means the method is available to any other Class or Object in the world. private means the method is only for internal object use, but cannot be called by other Objects. When you click on an object on BlueJ's Object bench, you can see the public methods but not the private ones since no one outside the Object has access to them.

Return Type
String means the method returns an item of type String. In Java, every method needs to declare its return type. It can either be an Object, a data type like int or boolean, or it can be void which means it returns nothing.

Method Name
getStudentID is the name of the method, which will be used when called elsewhere in the code. You should use descriptive names so your code will be easier to understand. The method name cannot have spaces in it, so multiple words are combined together with camelCase, where each word begins with a capital letter.

Challenge
Change the method getStudentID so it returns a String that says "ID: " followed by the student ID. For example, if the ID was "00234", it should return a String ID: 00234.

Compiling Code
Java code needs to be compiled to run. You can compile the code by clicking on the compile button on the top of the code editor. You can compile all the classes in a project at once by clicking on Compile in the main window.

The compiler will check for certain types of errors when it compiles the code, which are called "compile errors". When it finds an error, it will stop the compilation and tell you the error. This error-checking is useful for quickly finding errors before you even run your project.

When you compile code, all the objects in the Object bench will disappear. This is because their "blueprints", the Classes, have changed so their current form is incorrect. The Object bench is just for trying things out. If you want Objects to be created whenever the code is run, you will need to write code to create them.

Help with Errors
When you get an error, the compiler will display a message on the bottom. These messages are often not very clear for beginners, so BlueJ often provides an explanation of them. You can view the explanation by clicking on the question-mark next to the message.

BlueJ-error-message

Challenge

Put in the exact line of code needed to change getStudentID() so it returns a String with "ID: " before the ID.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

The compiler will throw a compiler-error if a method is missing a return type or if it fails to return the declared return type. Which 2 of the following methods will cause a compiler-error?

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

All Node Comments
Contact Us
Sign in or email us at [email protected]