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
Classes and Objects in BlueJ
Premium Content - Free Preview
This node will cover the basics of using BlueJ and connect it to using Java. You can view a longer BlueJ tutorial on the BlueJ site.
More About BlueJ
BlueJ lets you interact with Objects in your code directly which helps when learning concepts or to try something out quickly. We will see different actions that you can do with BlueJ and also how its done with real code. You can skip the "real code" parts for now, and come back to them to reference later.
Opening a Project
Open up the People Project in BlueJ
- Find the folder where BlueJ is installed and open the examples folder inside.
On Windows, look in C:\Program Files (x86)\BlueJ or C:\Program Files\BlueJ.
If you cannot find it, download a copy of examples and unzip it.
- Open the people folder and click on the BlueJ package.
(You can also open a project from within BlueJ by clicking on Project > Open Project. )
BlueJ Class Diagram
You should then see the following screen (without the blue markup):
BlueJ lets you create and view Classes and Objects in a visual manner. The main pane shows a diagram of each Class in a project and their relationships with each other.
As mentioned before, Classes are like blueprints to define specific instances (or Objects) of code. To create an actual object, right-click on a Class and click on a "new" menu item. For example,
- Right-click on
Staffto get a menu.
- Click on the second menu item,
- You'll get a box asking you to name your new object, but you can leave the default suggestion and click OK.
Notice how an object now shows up on the object bench.
All you needed when creating that object was its name, but sometimes Objects ask for more information so they can start off with certain data. To see this, let's create one more Object, this time a Student:
- Right-click on the Student Class
- This time, click on
new Student(String, int, String)
- You will now be given a prompt for both an Object name and parameters. You can copy the values shown below, and then click OK. Note there are two String values, which need to be enclosed in quotes.
This time, you were prompted to fill-in the parameters of the Object, so it could set up its initial values.
To create an actual object, you use the
new keyword. This will call up a constructor, a method defined in the Class that "constructs" an instance of the Class. A Class can have different Constructors, each with different parameters. When you create a new object, you need to pass it the parameters specified by its Constructor. The above objects could have been created/instantiated with the following actual code:
staff1 = new Staff(); student1 = new Student("Alice", 1992, "00234");