Java Language Basics

Java 101: Classes and objects in Java

Learn how to make classes, fields, methods, constructors, and objects work together in your Java applications

Classes, fields, methods, constructors, and objects are the building blocks of object-based Java applications. This tutorial teaches you how to declare classes, describe attributes via fields, describe behaviors via methods, initialize objects via constructors, and instantiate objects from classes and access their members. Along the way, you'll also learn about setters and getters, method overloading, setting access levels for fields, constructors, and methods, and more. Note that code examples in this tutorial compile and run under Java 12.

Class declaration

A class is a template for manufacturing objects. You declare a class by specifying the class keyword followed by a non-reserved identifier that names it. A pair of matching open and close brace characters ({ and }) follow and delimit the class's body. This syntax appears below:

class identifier
{
   // class body
}

By convention, the first letter of a class's name is uppercased and subsequent characters are lowercased (for example, Employee). If a name consists of multiple words, the first letter of each word is uppercased (such as SavingsAccount). This naming convention is called CamelCasing.

To continue reading this article register now