Java Language Basics

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.

download
Download the source code for example applications in this tutorial. Created by Jeff Friesen for JavaWorld.

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
}

To continue reading this article register now