Working with Objects in Java
Objects are to classes what variables are to data types. While classes define the structure and possible actions of a certain data type, objects are actual usable parts of the computer memory containing that data. The action of creating an object is known as making an instance of a class. In a sense, it is like making a copy of the template and then modifying it by accessing its variables or methods. Let's see this in action:
Computer myPC = new Computer( 2.5 );
myPC
is the actual object. We would say that myPC
is an object of the class Computer
in colloquial terms.
The different fields and methods inside the class can be accessed by typing the name of the object followed by a period and the name of the variable or method you want to address. Making any changes to the variables or calling the methods will take effect only within the scope of that object. If you had more objects of the same class in your program, each one of them would have...