- Objects
- Classes
- Inheritance
-
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
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
The Code for Creating and Using Objects
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:
- Open up the terminal by clicking on View > Show Terminal.
- 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:
- Create a new Staff instance using the "new" action without parameters.
- 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
) . - Invoke the getStudentID() method from the Student instance that you just created.
When you're done, you should see something like this:
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.
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)
int
and assigning an Object like staff1
. When you assign an int
to a variable, its analogous to putting a number in a box. The computer sets aside actual space to hold the number.

However, you cannot put an Object in a box, since there's no way to know how big to make the box. Instead, an Object variable stores a reference to the actual Object which is kept elsewhere. This means that multiple variables can all point to the same Object. Here's a nice diagram, but don't worry if you're confused:
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:
- Create a database instance
- 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
Car
s 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
Alex Opdenaker
Mar 13, 8:38 AMwow much hard
jack
Mar 13, 8:39 AMwow much hard such programming
Jack Kelly
Mar 13, 8:39 AMMuy dificil!!!
jack
Mar 13, 8:45 AMhow you make car accelerate
Learneroo
Mar 13, 10:31 AM@jack, see hint for creating the car. See the mini-Blob challenge above for calling a method on an object. Remember the
.
,()
and;
.jack
Mar 14, 9:19 AMCar car1 = new Car("green");
car1.accelerate();
car1.accelerate();
car1.accelerate();
xiaochayou
Apr 22, 3:27 AMIt's good to learn in this way!!
jay eve
Jul 5, 11:16 AMThis:
Blob instances have a method blop. Invoke the blop() method of blob1 in a correct line of code
confuses me, is the blop a typo????
Learneroo
Jul 5, 11:49 PM@Jay, no typo. Blobs can blop().
Gary Girish
Jul 19, 2:44 AMError: cannot find symbol - class staff
Gary Girish
Jul 19, 3:31 AMCroned
Jul 31, 5:54 PMWhen you call car.color, you are trying to directly call a private variable, which you can't do from outside of the object (hence why it's private). To set it's value you have to pass in the
String "green" as a parameter when creating the object (also, capitalize the class name, Car): Car gary = new Car("green");
Croned
Jul 31, 5:59 PMAlso, I didn't even notice this at first, but when you call an object's variable (that is public), you are going to want to reference the object's name you created it with, and not the name of the class. In your case, if .color was public, you would want to call gary.color and not car.color
Gary Girish
Aug 1, 12:50 AMthanks for that it helped:)