Writing a class
Now you write classes of your own. The example in this module is a class for the plants in a garden, where every plant has a name and a height in centimeters.
The class and its file
A class starts with public class and its name, and everything in it goes between its braces:
public class Plant {
}
- A
public class Plantmust be in a file calledPlant.java: the same name, capitals and all, plus.java. That's whyMainhas always been inMain.java. - Class names start with a capital letter, and so does each new word in them:
Plant,GardenBed,ArrayList. Variables and methods start with a small letter. - The files of a program go side by side: in the editor as tabs, on your own computer in the same folder.
- A class without
publicin front may share a file with another class. The practice drills show their small classes that way, afterMainin the same file, but in the lessons every class has a file of its own.
Instance variables
The data that every object carries is declared at the top of the class, inside its braces but outside any method:
public class Plant {
private String name;
private int height;
}
These are instance variables (also called fields). An instance is an object made from a class, and each instance gets its own copy of every instance variable: one plant can be a 30 cm fern while another is a 12 cm cactus. An instance variable lives as long as its object does, while a method's own variables disappear when the method ends.
Everything inside a class is indented one level (4 spaces), just as the lines of a method are indented inside the method.
private
private means that only the code inside the Plant class can use the variable. Code in Main can't read it or change it:
public class Main {
public static void main(String[] args) {
Plant fern = new Plant();
fern.height = 30;
}
}public class Plant {
private String name;
private int height;
}Main.java:4: error: height has private access in Plant
fern.height = 30;
^
1 errorWhy lock the data away? Because then the class decides what happens to it. When every change to height has to go through the class's own methods, the class can make sure that no plant ever gets a height of -20. Code outside the class uses a Plant through its methods, the way you use an ArrayList through add and get without touching its insides. Hiding the data like this is called encapsulation, and in this course instance variables are always private.
A class that does nothing yet
The class above already works as a blueprint: Plant fern = new Plant(); builds a Plant object. When a class has no constructor of its own (that's the next step), Java gives it an empty one, called the default constructor, and that's what the empty parentheses call. The instance variables then start at default values: 0 for numbers, false for a boolean, and null for a String or any other object (null means "no object at all"). But this plant can't do anything yet: it has no methods, and nothing outside it can set its name or height. The next steps give it a constructor and methods.