- 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
Premium Content - Free Preview
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)
End of Free Content Preview. Please Sign in or Sign up to buy premium content.
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:)