Tech and Media Labs
This site uses cookies to improve the user experience.

Java Classes

Jakob Jenkov
Last update: 2015-03-03

As you saw in the text about the Java main method tutorial, the execution of a Java program starts in the main() method of a Java class. But classes are more than just the execution starting point of a Java program.

Java classes are a mechanism used to group data (variables) and Java code (methods) which belong together into coherent "modules". Grouping variables and operations on these variables into Java classes makes it easier to structure your Java program when it gets too big to fit comfortably inside a single Java file (inside a single Java class).

Java comes with a lot of classes predefined for you, but you can also define your own Java classes. In fact, as soon as your Java program grows in size, it is a good idea to start breaking the code into different Java classes.

A Java class can contain:

  • Fields
  • Constructors
  • Methods

Fields are variables (data) that are local to the class, or instances (objects) of that class. I will get back to instances later. Fields are covered in more detail in my Java fields tutorial.

Constructors are methods that initialize an instance of the class. Constructors often sets the values of fields in the given instance. Constructors are covered in more detail in my Java constructors tutorial.

Methods are operations that the class or instances of that class can perform. For instance, a method may perform an operation on input parameters, or change the value of fields kept internally in the object etc. Methods are covered in more detail in my Java methods tutorial

Not all Java classes have fields, constructors and methods. Sometimes you have classes that only contain fields (data), and sometimes you have classes that only contain methods (operations). It depends on what the Java class is supposed to do.

Defining a Class in Java

All it takes to define a class in Java is this:

public class MyClass {


This defines a public Java class called MyClass. The class has no fields, constructors or methods.

.java Files

The above class definition should be put in its own file named Java files should be named the same as the name of the class they contain, with the .java as file name extension. Make sure you keep the same uppercase and lowercase characters from the class name in the file name too.

Only put a single class definition in each Java file, unless your class contains inner classes of some kind. Inner classes are covered in my Java nested classes tutorial.

Classes With Fields

The next example shows a Java class which is to model a car. Therefore the class has named Car and has three fields. Here is the Java class in code:

public class Car {

    public String brand = null;
    public String model = null;
    public String color = null;


This code defines a Java class named Car. The Car class has three fields. The Car class has no methods. Only field declarations. Fields are described in more detail in the text on Java fields.

Classes With Methods

Let's add a method to the Car class.

public class Car {

    public String brand = null;
    public String model = null;
    public String color = null;

    public void setColor(String newColor) {
        this.color = newColor;

In the class definition above I have added a setColor() method. When called, this method sets the internal color variable (field) to a new value. Methods are described in more detail in the text on methods.

Classes and Objects

A Java class is a template for how objects of that class looks. In other words, the Car class in the previous section is a template for how Car objects look.

To create objects of a certain class, you use the new keyword. Here is an example:

Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();


This example creates 3 Car variables, and assign a new instance of the Car class to each variable. Each variable now references a Car object.

After creating the 3 Car objects, the setColor() method is called on each object. Now the color (represented as a text) is set individually for each Car object.

Creating an object of a certain class is also called "instantiating" an object. The object is thus also called an "instance" of the given class. For instance, each of the Car objects above are also called an instance of the Car class, or simply "Car instances".

Further Concepts for Java Classes

What you have seen in this text only covers the very basics of Java classes. You need to learn about fields, constructors, methods nested classes, abstract classes, inheritance, access modifiers and interfaces too. All of these concepts are discussed in their own texts.

Jakob Jenkov

Copyright  Jenkov Aps
Close TOC