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

  1. 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.
  2. 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): Blue-J-screen

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.

Creating Objects

View animation

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,

  1. Right-click on Staff to get a menu.
  2. Click on the second menu item, new Staff().
  3. You'll get a box asking you to name your new object, but you can leave the default suggestion and click OK.

BlueJ_Create_Object

Notice how an object now shows up on the object bench.

BlueJ_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:

  1. Right-click on the Student Class
  2. This time, click on new Student(String, int, String)
  3. 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.

BlueJ_object_with_parameters

This time, you were prompted to fill-in the parameters of the Object, so it could set up its initial values.

Real Code


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Comments

  • I got Error when attempting task step 2: Cannot find symbol - variable student1

  • @niveoserenity, sorry if it wasn't clear, did you create a Student Object called student1 first? (as described above) ?

  • Task 1 at the end: do I do it by creating a new class for instance: right click -> new class -> and name it database1 or what ? if yes why I dont have there any otpions for adding like addperson, but basic Open editor, compile, remove, inspect? thanks

  • @calo, you need to create an instance (an Object) of the class Database. So right-click on the icon of the Database class to create an instance of it. (See "Creating Objects" above.)

  • When i open the package the first message i see is: "Initialising virtual machine...Done. "

    At this point i still see the diagonal stripes.

    I click on "Compile" and see the message: "Compiling...Done"

    cont...
  • Well one lunch after finally found out why it was only compiling Staff.

    Step 2 from instructions state to go to examples/people and click on the BlueJ package.

    cont...
  • -_- nope didn't work.... scratch last comment.

  • So solution for my last 3 comments was... used Windows 8 rather than Windows 7. = )

  • i dont have these examples

  • Where is the examples folder in linux?

  • where are located these examples in linux

  • @Jake, I added a download link to the examples folder above.

  • When I create student it create a student like student() and not like new Student(String name, int yearOfBirth, String studentID)

  • The only thing I am getting when I bring up a new database is "inherited from object" and 2 void methods - help!

  • Please help someone, i cant find the listAll anywere!??

    When i compile al previous steps are gone..... And when i dont i cant find listAll either. What to do?

    step for step please?

  • I cant figure this out!

    It does not word for me, i cant find listAll please help. step by step?

Contact Us
Sign in or email us at [email protected]