Java Reference
In-Depth Information
a return value, it is not a constructor. This Camera class does not have a constructor that
takes in an int , so the compiler generates the following error:
Camera.java:13: cannot find symbol
symbol : constructor Camera(int)
location: class Camera
Camera c = new Camera(60);
^
1 error
The Default Constructor
Every class has a constructor. If you do not explicitly defi ne a constructor for a class, then
the Java compiler inserts a default constructor for you. The default constructor takes in no
arguments and has an empty method body.
For example, suppose we have a class named Tomato with the following defi nition:
1. public class Tomato extends Fruit {
2. private double weight;
3. private boolean ripe;
4.
5. public void setWeight(double w) {
6. weight = w;
7. }
8.
9. public double getWeight() {
10. return weight;
11. }
12.
13. public void setRipe(boolean b) {
14. ripe = b;
15. }
16.
17. public boolean isRipe() {
18. return ripe;
19. }
20.}
Because the Tomato class does not explicitly defi ne a constructor, the compiler generates
one that looks like the following:
public Tomato() {
}
Search WWH ::




Custom Search