Java is an older language built with the Object-Oriented Programming language structure, meaning most — if not all — things in Java are objects. Given that OOP languages are very popular, they tend to be robust languages with many features. In fact, even classes are objects in Java and in this post, you will be learning more about them and why they matter.

This post will cover Java classes and what they mean to you as a developer. You will also learn the syntax, structure, requirements, and behavior of Java classes. By the end of this, you will have a basic understanding of Java classes and how to leverage them in your software.

You will also see a couple of videos and code examples to help drive the concepts home.

Without further ado, let’s dive right in.

Download Now: Free HTML & CSS Hacks

What is a class in Java?

Java classes are an essential part of the language, next to objects. Everything is associated with either a class or an object in Java, including class instances. Let’s break this down a little further with the simple analogy of a real-life object.

Consider pets, for example; a pet can have attributes that describe its existence. For example, a pet can be a cat, dog, or lizard. In addition, that object can have sub-objects like color, weight, breed, and more attributes. This is where classes come in; they allow you to create a blueprint for your pets. This blueprint will enable you to create multiple pets with the same attributes but different values.

With these structures in place, we can quickly map out instances of individual pets based on shared attributes and subclasses. For example, a business might use classes to identify types of clients or transactions. Each instance will have unique information such as names, prices, quantities, etc. The following video shows how this process works.

How to Create a Class in Java

In Java, a class has several working parts that need to be in place to work correctly. There are required and optional parts within a class declaration, and each serves a unique purpose.

Let’s look at each of these crucial pieces of the puzzle in more detail below.

  • Modifiers: A class modifier dictates the type of access a class can have, which can be public or have default/private access. A public modifier allows a class to be accessible from within other classes, while default prevents it.
  • Class keyword: The class keyword creates a class declaration needed for the compiler to identify the creation of a new class.
  • Class name: The name should begin with an initial letter (capitalized by convention), and the standard is for the class filename and class name to be the same.
  • Superclass(if any): The name of the class’s parent (superclass), preceded by the keyword extends. A class can only extend (subclass) one parent. Not all classes will have a superclass associated with them.
  • Interfaces(if any): A comma-separated list of interfaces (static constants and abstract methods) implemented by the class, preceded by the keyword implements. A class can implement more than one interface.
  • Body: The class body is surrounded by braces, { } which identify the body of the class. The body of the class is where the behavior, interfaces, and actions are defined.
With each of these parts of the class in place, you will be able to declare your first class, which might look something like the following.

Java Class Syntax Example

In Java, when you declare a class, you will need to make sure you have everything in place, and you will need to make sure the file name is the same as the class name. Naming the files and classes like this is done so that the Java Virtual Machine can identify the location of the class.

 
public class Person {
  String name = "Frank Reynolds";
  public static void main(String[] args) {
    Person myObj = new Person();
    System.out.println(myObj.name);
  }
}

The code above creates a public class named Person with the name attribute. The name attribute is of the string type and holds “Frank Reynolds” as its value. Next, we declare the main method, which informs the JVM of the starting/entry point of the class.

The main method is where the class can start doing things with its information. In this case, the action taken is to create a new object of the Person class and then print the value of the class attribute name.

Classes and Objects in Java

Java classes and objects are not the same, though they have a deep relation. When a class is instantiated in Java, an object is created — or initialized. This process is similar to creating a new instance of a person. Each time a new object is made from the class, a new “Person” is created.

Each new Person would have the same attributes, behaviors, and methods but contain different values. When creating several new instances — or class objects — of a Person, each will have a name that would likely be different. Take a look at the following video, which elaborates on how classes and class objects work.

How to Create an Object in Java

Now that we have clarified the difference between a class and a class object, let’s discuss how to instantiate a new class object. To create a new class object, you will need to follow one of four approaches. Let's look at a couple of them individually to understand better how each works.

One of the more popular ways to create a class object is to use the new keyword, which you have already seen above.

This approach is pretty straightforward; it identifies the type of object (Person), specifies the variable's name, and finally assigns a new Person to that variable.

 
Person obj = Class.forName("com.p1.Person").newInstance();

Getting Started With Using Java Classes

Java classes are powerful and a handy tool to have on your developer tool belt. Of course, building on this knowledge and honing your skills with classes will take practice and due diligence. However, you are well on your way with the information you’ve learned here.

New Call-to-action

 css introduction

Originally published May 20, 2022 7:00:00 AM, updated May 20 2022