September 2011
Beginner
650 pages
15h 47m
English
In the preceding examples, the instance variables of each Vehicle object had to be set manually using a sequence of statements, such as:

An approach like this would never be used in professionally written Java code. Aside from being error prone (you might forget to set one of the fields), there is simply a better way to accomplish this task: the constructor.
A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. Typically, you will use a constructor to give initial values to the instance variables defined ...
Read now
Unlock full access