What a class is
Java comes with types such as int, String and arrays. A class lets you add your own type to that list. It is a blueprint that describes what a thing knows (its data) and what it can do (its behavior). A Dog, a BankAccount or a Game can each be a class.
Without classes, a program about dogs would keep each dog's name in one variable, its age in another, and write separate code for each dog's actions. With a class, all of that lives under one name, Dog, and the rest of the program works with whole dogs instead of loose variables.
A class is not a dog, only the description of one. Real dogs, called objects or instances, come from the next lesson, where new builds them from the blueprint.
The syntax
The smallest class you can write is the keyword class, a name and a pair of curly braces:
class ClassName {
}
classtells the compiler that you are declaring a new type.ClassNameis the name you choose. Java programmers write class names in PascalCase: every word starts with a capital letter and there are no underscores, as inDog,BankAccountorHighScore.- The braces hold the body of the class. Fields and methods go there, and for now the body can be empty. An empty class is still a valid class.
Names are case-sensitive, so Dog and dog are different identifiers. The naming convention is not enforced by the compiler, but code that ignores it is harder for other Java programmers to read, because a capitalized name tells them at a glance that it is a type and not a variable.
Where the body goes later
Everything a class contains sits between its braces. The lessons that follow fill that space in this order:
- Fields hold the data of each object, such as a name and an age.
- Constructors set that data when an object is created.
- Methods are the actions an object can perform.
A comment placeholder is a handy way to mark that spot while you plan:
class Dog {
// fields and methods will go here
}
The lesson's exercise, step by step
The starter code already has a Solution class whose mainFunction uses a Dog, but no Dog class exists yet:
class Solution {
public void mainFunction() {
Dog dog = new Dog();
System.out.println("Defined the Dog class!");
}
}
As it stands, this fails to compile. The compiler reaches Dog dog, finds no type with that name and reports that it cannot find the symbol. The fix has one step: declare an empty class named Dog above Solution, with the class keyword, the PascalCase name and a pair of braces.
Here is the complete reference solution:
class Dog {
}
class Solution {
public void mainFunction() {
Dog dog = new Dog();
System.out.println("Defined the Dog class!");
}
}
Once Dog exists, the line Dog dog = new Dog(); has a type to work with. It declares a variable named dog of type Dog and creates an object to store in it. The program then prints Defined the Dog class!.
Details worth knowing
- One file, several classes. A Java file may hold more than one top-level class, which is why
DogandSolutioncan sit side by side here. A top-level class markedpublicis different: it must be the only public class in its file, and the file must have the same name, sopublic class Dogbelongs inDog.java. The classes in this lesson have no access modifier, which is fine for small exercises. - Order doesn't matter. Java lets
SolutionuseDogeven ifDogis written after it. The lesson putsDogfirst because that reads naturally. - No semicolon after the closing brace. Unlike C++, a Java class declaration doesn't end with a semicolon, so leave it out.
- A class is a type. Once defined,
Dogcan be used wherever a type is allowed: variable declarations, method parameters, return types and array elements such asDog[]. - The compiler adds a constructor. You wrote no constructor, yet
new Dog()works. When a class declares none, Java supplies a default one that takes no arguments. The constructors lesson explains this in detail.
Common mistakes
- A different name. Naming the class
dog,DogsorDOGleavesDogundefined, andnew Dog()still fails with "cannot find symbol". - Missing braces.
class Dogalone is not a complete declaration. The compiler expects a body, even an empty one. - Underscores in the name.
bank_accountcompiles, but the Java convention isBankAccount. - Putting the class inside a method. The class belongs at the top level of the file, outside
Solutionand its methods, as in the solution above. - Confusing the class with an object. Defining
Dogdoesn't create any dogs. Onlynew Dog()does.
Key takeaways
- A class is a blueprint that defines a new type, and it bundles related data and behavior under one name.
- The syntax is
class Name { }, and the body may be empty. - Class names use PascalCase and are case-sensitive.
- Fields, constructors and methods go inside the braces, and the next lessons add them to
Dog. - A class describes objects. You create them with
new.