Our first class and first object

So what exactly is a class? A class is a bunch of code that can contain methods, variables, loops, and all other types of Java syntax. A class is part of a package and most packages will normally have multiple classes. Usually, but not always, each new class will be defined in its own .java code file with the same name as the class.

Once we have written a class, we can use it to make as many objects from it as we need. Remember, the class is the blueprint, and we make objects based on the blueprint. The house isn't the blueprint just as the object isn't the class; it is an object made from the class.

Here is the code for a class. We call it a class implementation:

public class Soldier { int health; String soldierType; ...

Get Learning Java by Building Android Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.