Search⌘ K
AI Features

Introduction to Classes

Explore the concept of classes in Java, including their syntax, constructors, and methods. Understand how classes act as blueprints for data types and how to create and use them effectively, building a foundation for object-oriented programming.

We'll cover the following...

Recap

Since the Hello, World! program, we have been ignoring a couple of repeating lines in all of the Java codes we have seen so far. So, how long are we going to ignore this elephant in the room?

Java
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}

We only discussed the fact that line 5 is used to print Hello World! on the screen. But what about the rest of the lines?

Formally, the first line is constructing a class for the HelloWorld program, whereas the third line describes a Java method by the name of main.

What is a class?

Is it the same as the trigonometry class you had at school? Not at all! A class in Java is used to describe a type. Here, the word ‘class’ derives from classify. It is a way to classify different types of data.

💡 Note: Primitive data types are not classes. Recall that we learned about data types like int, double, and boolean, i.e., the primitive data types. These data types are not a product of a class. All the reference data types, however, are due to classes. In other words, data types in Java can either be primitive data types or a class.

A class is a blueprint for how a certain data type can work. For example, a class describes what we can do with a variable of the said class. A class consists of different constructors and methods.

  • A constructor is responsible for the creation of a variable of that class’s type.
  • A method is something that describes what a variable of the class’s type can do.

A class can have multiple constructors and methods.

There are different classes in the Java programming language. Some of the classes you have already seen are: String and System. You will get to see more classes in future chapters!

Class syntax

The following is the basic syntax to define a class:

class name
{

}

We write the keyword class and then give a meaningful name of our choice. Then we add {}. All of the constructors, methods, and variables related to a class are written inside the {}.

We are already familiar with how a basic class works, e.g., the HelloWorld class. Let’s look at the following class, which has a little more than just a main method.

Java
// Creating a class
class Introduction
{
// A class can have its own variables
String name;
// This constructor requires the user to pass a String type variable or value
Introduction(String enteredName)
{
// we can read/update the value of a class's variable in the constructor
name = enteredName;
}
// This class has the following method
public void greet()
{
// we can read/update the value of a class's variable in the class's methods
System.out.format("Hello %s! This is AP CS A course.", name);
}
}

We have declared a class named Introduction here. This class has one String type variable, called name (see line 5). We can read and update this variable in all constructors and most methods.

Lines 8-12 define a constructor of this class.

⚙️ Note: Constructors should always have the same name as the name of the class.

Notice how at line 8, the constructor gets a String type variable, called enteredName. This variable is being passed as an argument to this constructor. So, when we create an Introduction type variable, we will have to pass a String type variable.

Lines 15-19 declare a method, called greet. At line 18, we use the format call to print to the screen. This call takes the variable, name, as an argument. As we mentioned earlier, most methods can use the variables of the class; this is one such method.

📌 Note: It’s a good practice to name a method in such a way that it describes the functionality of the method.

This was a basic class. We will learn how to use this class in the next lesson.