Java Classes
Java Classes
As you saw in the text about the Java main program, the execution of a Java program starts in the
main()
method of a class. But classes are more than just the execution starting point of a Java program.
Java classes are a mechanism used to group data (variables) and Java code (methods) together into coherent "modules". A class typically contains:
- Fields
- Constructors
- Methods
Fields are variables (data) that are local to the class, or instances (object) of that class. I will get back to instances later.
Constructors are methods that initialize an instance of the class. Typically a constructor sets the values of fields in the given instance.
Methods are operations that the class or instances of that class can perform. For instance, a method may perform an operation on input parameters, or change the value of fields kept internally in the object etc.
Not all classes have both fields, constructors and methods. Sometimes you have classes that only contain fields (data), and sometimes you have classes that only contain methods (operations). It depends on what the class is supposed to do.
Defining a Class
All it takes to define a class in Java is this:
public class MyClass { }
This defines a public class called
MyClass
. The class has no fields, constructors or methods..java Files
The above class definition should be put in its own file named
MyClass.java
. Java files should be named the same as the name of the class they contain, with the .java
as extension. Make sure you maintain the uppercase and lowercase characters from the class name, in the file name too.
Only put a single class definition in each Java file, unless your class contains inner classes of some kind. Inner classes are covered in a later text.
Classes With Fields
The next example shows a class which is to model a car. Therefore the class has named
Car
and has three fields.public class Car { public String brand = null; public String model = null; public String color = null; }
This code defines a class called Car. Thus a Car object has three fields. The class has no methods. Only field declarations. Fields are described in more detail in the text on fields.
Classes With Methods
Let's add a method to the
Car
class.public class Car { public String brand = null; public String model = null; public String color = null; public void setColor(String newColor) { this.color = newColor; } }
In the class definition above I have added a
setColor()
method. When called, this method sets the internal color variable to a new value. Methods are described in more detail in the text on methods.Classes and Objects
A Java class is a template for how objects of that class looks. In other words, the
Car
class in the previous section is a template for how Car
objects look.
To create objects of a certain class, you use the
new
keyword. Here is an example:Car car1 = new Car(); Car car2 = new Car(); Car car3 = new Car(); car1.setColor("red"); car2.setColor("green"); car3.setColor("blue");
This example creates 3
Car
variables, and assign a new instance of Car
to each variable. Each variable now references a Car
object.
After creating the 3
Car
objects, the setColor()
method is called on each object. Now the color (represented as a text) is set individually for each Car
object.
Creating an object of a certain class is also called "instantiating" an object. The object is thus also called an "instance" of the given class. For instance, each of the
Car
objects above are also called an instance of the Car
class, or simply "Car instances".
0 comments: