The creation of classes in Java is a fundamental component of what is known as object-oriented-programming. Object-oriented programming is a paradigm (a style of programming) that is based on the use of objects that can send messages to each other.

To fully understand how to use classes in Java you will first need to understand what objects are.

Exploring Objects

In Java, the term object is often used interchangeably with the term class, which is understandable given that an object is created from a class.

A class can be thought of as a blueprint—so it contains all the information that is necessary to create an object.

For example, you might create a student class that will contain basic information on a student, such as a name, age, and course of study. Each time a new student is created using the student class that student is referred to as an object.

Creating a Class in Java

The creation of classes in Java is necessary because they give your program structure, and reduce the amount of code that is present in your program. Instead of creating a new state and behavior for each similar object in a program, you can simply call the class that has the template for the creation of that object.

In a Java class, one of the most important statement is a class declaration.

Class Declaration

As a general rule, every class in Java is declared using the keyword “public”, which indicates that the class in question can be accessed by other classes in the Java program. The “class” keyword follows this and serves to indicate that the Java statement that you are creating is a class.

Next is the class name, which generally starts with a capital letter and can be any name that you think is appropriate for the objects that you wish to create. In the example below the name of the class is student, because the intention is to create student objects from this class.

Example of a Class Declaration in Java

        public class Student {
}

The final component of a class declaration is the open and close curly braces. The first curly brace signifies the start of the class, while the second curly brace signifies the end of the class. Therefore, every state and behavior that is unique to our class will be stored between these curly braces.

Using curly braces helps to add structure to your Java code. This feature should not be taken for granted as other languages, such as Python, do not use curly braces to structure code when creating classes.

Related: How to Create a Simple Class in Python

Java Class Attributes

Attributes can be considered as building blocks for a Java class; they contain the data elements that are used to give an object its state and are often referred to as variables.

Our class is called “student” and is focused on storing information of students that belong to a particular college/university. Therefore, the information on file could be each student’s name, age, and course of study.

Example of a Class With Attributes

        public class  Student {
//variable declaration
private String fname;
private String lname;
private int age;
private String courseOfStudy;
}

There are a few important things to make note of in the program above. When declaring an attribute/variable in Java you need to have an access modifier, a data type, and the variable name.

In our program, the access modifier is the keyword “private”, which is used to prevent external access to the data in the student class, and this is a good programming practice because it protects the integrity of the data that is stored in a class.

There are two different representations of data types in our program—String and int.

  • The String keyword is used to declare variables that store text data and needs to begin with an uppercase “S” to be recognized by the Java compiler.
  • The “int” keyword is used to declare attributes that store integer data and should be in all lowercase because the Java Programming language is case sensitive.

The name of the variable is usually the last part of an attribute/variable declaration. However, the value of a variable can be assigned to it during the declaration stage. After all variables are declared, you can move to the creation of constructors.

Java Constructors

No class in Java is complete without a constructor---it is a core concept of the language. A constructor is a method in Java that is used to give an object its state and is called automatically when an object is created. Now there are three types of constructors: default, primary, and copy.

When an object is created from a class you can choose to either provide what is known as parameters (values that can be passed to a method) to the object or you can create it without any parameters.

If a new object is created from a class and is not given any parameters then the default constructor will be called; however, if parameters are provided then the primary constructor will be called.

Example of a Class With a Default Constructor

        public class  Student {
//variable declaration
private String fname;
private String lname;
private int age;
private String courseOfStudy;
//default constructor
public Student() {
fname = "John";
lname = "Doe";
age = 20;
courseOfStudy = "Pyschology";
 }
}

In the code above our default constructor is assigned the “public” access modifier, which enables it to be accessed outside of the student class. Your constructor access modifier must be “public”, else your class will not be able to create objects using other classes.

Constructors are always assigned the name of the class that they belong to. For a default constructor, the class name is followed by parentheses as demonstrated in our code above. The parentheses should be followed by open and close curly braces that will contain the default assignment of the variables that belong to the class.

From our code example above, whenever an instance of the student class is created without parameters the default constructor will be called and a student with the name John Doe, the age of 20, and a psychology course of study will be created.

Example of a Class With a Primary Constructor

        public class Student {
//variable declaration
private String fname;
private String lname;
private int age;
private String courseOfStudy;
//default constructor
public Student() {
fname = "John";
lname = "Doe";
age = 0;
courseOfStudy = "Pyschology";
}
//primary constructor
public Student(String fname, String lname, int age, String courseOfStudy) {
this.fname = fname;
this.lname = lname;
this.age = age;
this.courseOfStudy = courseOfStudy;
  }
}

The main difference between a default and a primary constructor is that the primary constructor takes arguments, while the default constructor does not. To use the primary constructor of the student class you will need to provide the name, age, and course of study for the student object that you wish to create.

In the primary constructor, each data attribute value that is received as a parameter is stored in the appropriate variable. The “this” keyword is used to indicate that the variables that they are connected to belong to the student class, while the other variables are what are received as parameters when an object of the class is created using the primary constructor.

The copy constructor is a copy of the primary constructor and is not necessary for the successful execution of your Java program; therefore, there is no need to include it.

Now You Can Create a Simple Class in Java

This articles shows how to not only create a useful class in the Java programming language, but also some fundamentals of object-oriented-programming. This includes the creation of variables and exploring the "Sting" and "int" data types, and understanding the public and private access modifiers in Java.

Image Credit: Christina Morillo/Pexels