Introduction to Java: Classes

When it comes to classes and object-oriented programming languages, they represent the basic building blocks. In other words, the entire codebase is organized in classes.

Furthermore, the class itself should be placed in the file with the .java extension.

In this tutorial, we will learn about classes in Java.

Simply put, we can think of a class as a blueprint for building objects. It is important to note the class and object do not represent the same thing. We create objects from classes.

Inside a class, we put variables and methods. Together, they are called members of the class.

1. Creating a Class

Java comes with many helpful classes we can use in our project, such as String, BigDecimal, etc. We can freely use them in our code. However, most of them represent basic data types we will use to create our own classes.

Why do we need custom classes?

Let us consider the following scenario. Suppose we want to create a class that will represent a cat. Within Java API, there is no such class that will represent a cat. You might be tempted to use the String class, but we would not be able to store all the information we want in one variable.

Say we would like to get the information about the cat’s name, color, and age. We cannot save three pieces of information into a single variable. Thus, we need some other structure – our own class.

Firstly, we need to create the Cat class that will represent the entire cat:

package dev.beenary;

public class Cat {

Secondly, we want to store the characteristics (name, age, color) of the cat. We will map those characteristics into fields and put them inside a class:

package dev.beenary;

public class Cat {
    String name;
    String color;
    int age;

Next, we would think about the things a cat can do. For instance, sleeping, eating, and making sounds. We will create a method for each of the actions cat can perform:

package dev.beenary;

public class Cat {
    String name;
    String color;
    int age;
    void sleep(){
        System.out.println("I am sleeping");
    void eat(){
        System.out.println("I am eating");
    void makeSound(){

Finally, our blueprint for cats is finished. We can use one class to create multiple objects (of the same type). 

1.1. Creating a Class – Rules

When creating a class, we need to follow certain rules:

  1. We can create at most one class with the public access modifier inside one file.
  2. Moreover, inside one file, we can define multiple classes, however, only one of them can be publicly accessible.
  3. When we have a public class, the file name and the class name must match.
  4. A class should be declared using the class keyword.
  5. The name of the class should follow a standard naming convention – CamelCase format.

If we forget certain rules, the compiler will remind us by giving us an error.

2. Parts of a Class

Now, let us demystify the parts of the class we created.

2.1. Package

Packages help us to structure our code. We use them to logically group classes that are somehow connected.

However, you can imagine packages like directories on your PC. You probably do not have all the files inside one directory. For instance, if you are attending college, you could have one directory for each course you are taking. In addition, you might have separate directories for lectures and exercises.

Similarly, the classes are organized inside packages.

The first line in the .java file tells us where our class lives:


Now, the package word is one of the reserved keywords in Java. After, we list the package name (or names, starting from the root src directory). Packages are separated with a dot (.).

It is important to note package declaration should always be the first line in our .java file.

Default Package

Postoji još jedan poseban paket, a to je onaj zadani paket (engl. default package). To je paket koji nema naziv. Ako kreiramo klasu u tom, vršnom paketu, imat će izostavljenu deklaraciju paketa. Prva linija u toj datoteci bit će import izjava (ako ju imamo) ili deklaracija klase (ako nemamo import izjave).

2.2. Defining a Class

We use the public word to note our class is publicly accessible. It is one of the access modifiers. Simply put, with the public access modifier, we can access the class from any other class inside out project.

public class Cat {

Additionally, the class keyword tells the compiler we want to create a class. Likewise, if we would like to create, for instance, an interface, we would use the interface word instead.

Finally, the Class word represents the name of our class. It could be anything your heart desires. However, it is recommended to name your classes with a noun. And, of course, it should make sense. In other words, it should describe the purpose of the class.

Furthermore, if a class is publicly accessible, the name must match the name of the file.

Lastly, the curly brackets represent the beginning and the end of our class. We should place our code inside those brackets.

Next, let us talk about members of a class.

3. Class Members – Variables

We use variables to store information about different objects. Each variable represents one characteristic of the future object.

In Java, we have three flavors of variables: instance, local, and static variables. In this section, we will focus on instance variables. Those are the ones placed inside the class.

Additionally, a variable must have type and name:

int age;

Here, the int is a data type and age is the name of the variable. Inside this variable, we could store numeric values.

Each statement in Java must end with a semicolon (;). It is mandatory and we should not forget it.

Additionally, besides type and name, each variable should have an access modifier.

4. Import Statements

Once we define a class, we can use it as a reference data type in other classes (just like we use the String class).

Now, most often, we are working with structured projects (and external libraries) where not all classes share the same package. Import statements come in handy when we would like to use classes from different packages we are in.

For example, suppose we would like to add a new variable where we will save the cat’s date of birth.

Java comes with two classes where we can store the date and time. They are both called Date, but they are placed in different packages:

It is important to note those are legacy classes. Nowadays, Java offers the java.time.LocalDate and the java.time.LocalDateTime to store date and time information. I am mentioning Date classes just as an example.

Additionally, we can see another usage of the packages – they allow us to have two classes with the same name.

Now, in order to use Date in our Cat class, we need to tell Java which of those two we would like to use. We could specify the one we will use with import statements:

package dev.beenary;

import java.util.Date;

public class Cat {
    Date dateOfBirth;

The import statements consist of the import word followed by the fully qualified class name. The fully qualified class name is made of the package name(s) and the name of the class.

Furthermore, we can omit the import statement and use a fully qualified name instead:

package dev.beenary;

public class Cat {
    java.util.Date dateOfBirth;

4.1. Unnesecarry Imports

We already mentioned Java comes with predefined classes. The set of those classes is called the Java API.

Some of those classes are automatically imported into our code – the ones from the java.lang package. Every class from this package will be automatically accessible in our code.

For instance, we do not need to import classes such as String, Object, Byte, Short, Integer, Long, Float, Double, Character, etc.

Furthermore, there is no need to import classes that belong to the same package as our class.

4.2. Wildcards

Moreover, using wildcards (*), we can import all the classes and interfaces from the same package:

package dev.beenary;

import java.util.*;

public class Cat {

    Date dateOfBirth;
    SimpleTimeZone someOtherClassFromUtilPackage;

Using wildcards, we imported all the classes placed in the java.util package.

However, let us see the rules we need to obey when working with wildcards:

  1. Using wildcards, we can import classes and interfaces that are in the package after which we placed *.
  2. We can not import methods and variables.
  3. We can use only one wildcard per import statement.

5. Key Takeaways

In this tutorial, we learned about classes in Java.

To summarize, you can think of a class as a blueprint for building objects. We need to create our own classes to represent specific structures. When creating a publicly accessible class, it should have the same name as the file.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x