Programming in Java on Your Computer
- Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Source Code and Methods
Premium Content - Free Preview
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.
This will open up the source code file:
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:
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.
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
boolean, or it can be
void which means it returns nothing.