Construction. Class Definitions
[comp1110-80]
Construction. Class Definitions
[comp1110-80]In Java, objects are instances of classes.
A class definition is a kind of data definition, similar to what we have seen before for enums.
However, classes are much more powerful, and they are used to model pretty much everything in our programs that cannot be modeled by a simpler data type.
The core parts we will deal with for now are fields, which contain an object's data, and methods, which define an object's behaviour.
The basic definition of a class consists of the keyword class and a class name, followed by curly braces, like so:
This does define a new type Person.
To create a new instance of it (i.e. an object), you could write something simliar to what we did for arrays, using the keyword new:
This is not very useful, because the Person class above has no data or behaviour, but you can add those.
By convention, class names in Java should be nouns that start with a capital letter, and use capital letters to mark the start of any word internal to the name.
For example: LectureNote .