A class is an abstract category of things. It is a blueprint or prototype that defines the values (variables) and the actions (methods) common to all objects of a certain kind.
An object is a particular instance of a class. Software objects are often used to model real-world objects you find in everyday life.
For example:
Class Object, or InstanceCars The Reed family Chrysler Minivan Songs The Star Spangled-Banner Student Ima Hogg Circle Blue circle in upper-left corner of screen MemorableDate The time my car broke down
It is helpful to visualize objects being created and used in BlueJ. Don't worry about understanding all the details, just get the general idea for now. We will revisit these concepts later.
Open the shapes project, and right-click to create class objects on the object bench.
Call the object methods.
(If you do not already have the BlueJ projects, you can download shapes.zip (see here for the contents of shapes.zip). Also see the downloads section of the notes and resources page if you want to download BlueJ at home.)
A class method is the name of an action associated with a class. For example, a circle could have a setColor() method, a car could have a getVIN() method, a song could have a displayAuthor() method, and a Student could have a displayBirthdate() method.
(Do a Google search on Ima Hogg...)
We have already seen the main() method. Your program can actually have many other methods as well. Methods can be used to divide up your program into meaningful pieces. For instance, you could have a method to display instructions for your program.Call a method with parameters. Parameters are used to modify an objects properties, for example in the Circle class we have the method changeSize( int newDiameter). If we had a circle object called circle1, and we wanted to change its diameter to 25, we would write the code:
circle1.changeSize( 25);Note the different types of parameters. The type specifies the kind of parameter that it is, such as int (short for integer, or whole numbers), or String (a sequence of text that could include spaces, specified using "double quotes").
For instance, we can change the color of a circle. Note that the parameter list for a method could be empty, if it doesn't have any parameters.Note that you can have multiple instances of circles, each with their own properties. These properties are more properly called fields, or variables, which store an object's state (set of properties).
Open up the Picture Project in BlueJ. Open the editor for the Picture and make a change.
You will need to recompile when you see the stripes under that class. (If you do not already have the BlueJ projects, you can download picture.zip or see the contents of it).Close that project and open up the lab-classes project (If you do not already have the BlueJ projects, you can download lab-classes.zip or see the contents of it). There could be multiple instances of class Student, and there could be multiple instances of class LabClass, each of which keeps track of a list of students. (Remember this is just a taste of things to come - I don't expect you to understand all of this right now.)
See how the getName() method has a return value of type String.