The Code for Creating and Using Objects


Collapse Content

Viewing the Code

Previously, you created Objects and called their methods using BlueJ's visual interface. When you did this, BlueJ would create and execute code that corresponded with your action.

To view the actual code that gets created you just need to:

  1. Open up the terminal by clicking on View > Show Terminal.
  2. With the Terminal selected, click on Options > Record method calls.

You can now repeat the same actions that you did in Objects in BlueJ, but this time you can see what code gets created:

  1. Create a new Staff instance using the "new" action without parameters.
  2. Create a new Student instance using the new action that takes 3 parameters. Fill it in however you want (though you should keep the name student1) .
  3. Invoke the getStudentID() method from the Student instance that you just created.

When you're done, you should see something like this:
bluej-and-terminal

Let's find out what these lines of code mean!

Creating Objects

In the previous module you saw how to declare variables and assign them to values. For example you could type:

int num;
num = 11;

Or alternatively you could declare and assign it in one line:

int num = 11;

You can do a similar thing when creating Objects. You declare the type and name of the Object variable:

Staff staff1;

Note that instead of declaring a primitive type (like int), you declare the Class (with a capital letter) that the Object belongs to.

object-variable

Once you declared an Object variable, you can assign it an object:

staff1 = new Staff();

This is something new, since you are creating a new Object. This is done with the new keyword. The staff1 variable will now point to this new Staff Object.

Constructors
Notice there are parentheses at the end of the code to create an Object. Parentheses are used in Java whenever a method is called. In this case, the parentheses represent the call to the constructor, a method that runs when an object is created. Constructors automatically set up certain values in an object. Some constructors have parameters, which are passed (i.e. assigned) inside the parentheses.

The declaration and assignment can again be done in one line of code, even though it now includes creating and setting up an object:

Staff staff1 = new Staff();
(hover above for more info)


Object Variable Details

Creating another Object
In the next line of code, a new Student Object was created:

Student student1 = new Student("Alice", 1992, "00234");

This is similar to the previous code, but here a constructor was called that took in 3 parameters. A class can have multiple constructor methods where each one takes in different parameters. When you create an object, you must choose one of its constructors, which you do by passing in the parameters that match that constructor.

Accessing Methods and Variables

Accessing methods of Objects
The third line of code returned the ID of the student: student1.getStudentID()

It used the "dot-notation". To call a public method of an object from code outside the Object itself, you use the "dot".

Accessing variables of Objects
To access the public variables of objects, you use the same dot-notation. For example, to access the color of a car1, a Car object, you would type car1.color. As mentioned, you usually should avoid directly accessing the variables of another Object.

Arrays
Remember Arrays? They were (simple) Objects so they also use dot-notation. That's why you can access the length variable of an array with this code:
array1.length;

Strings
Remember Strings? They looked like simple data types, but they required a capital letter in String to declare:

String str = "hello";

This is because all Strings are really Objects. String is a built-in Java Class and specific Strings are just instances of it. The String class comes with many useful methods, which will be covered later. Strings can be created in the same way as other Objects (with the new keyword), but Java comes with a built-in shortcut to create them faster, so it isn't necessary.

Objects as Parameters

As mentioned, you can pass in Objects as parameters to other methods. That's why String are able to be parameters. To see the code for this, repeat the Database actions you did before with the terminal open:

  1. Create a database instance
  2. Add a student to the database

This will show the following code:

Database database1 = new Database();  
database1.addPerson(student1); 

In the second line of code, a student was added to the database by passing in the name of the student as a parameter.

Finally you can print the data in the database with the listAll method. The terminal should then look like this:

database1.listAll();
Name: Alice
Year of birth: 1992
Student
Student ID: 00234

Practice

To get practice with creating and using objects, you should practice coding these actions again in the codepad. (Resume here when you're done; I'll wait :) However, all these actions are just for learning and trying out things. To create actual programs you will write need to write code like in the actual source files.

Challenge

Cars are Objects that have a color and speed. The color is a String which is set when the car is created, and the speed is an int which changes when the Car is accelerated. To accelerate a car by 1 unit, simply call its accelerate() method.

Create a "green" car and accelerate it to a speed of 3.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

In one line of code, create an Object blob1 of Class Blob that takes no parameters.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

Blob instances have a method blop(). Invoke the blop() method of the Blob blob1 in a correct line of code.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

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