Reviewed by: Juliano Rabelo
Last Updated: August 14, 2020

Definition - What does Class mean?

A class — in the context of Java — is a template used to create objects and to define object data types and methods.

Classes are categories, and objects are items within each category.

All class objects should have the basic class properties.

Core properties include the data types and methods that may be used by the object.

Techopedia explains Class

A class in Java is a logical template to create objects that share common properties and methods.

Hence, all objects in a given class must have the same methods or properties.

For example: in the real world, a specific cat is an object of the “cats” class. All cats in the world share some characteristics from the same template such as being a feline, having a tail, or being the coolest of all animals.

In Java, the “cats” class is the blueprint from which all individual cats can be generated that includes all cat characteristics, such as race, fur color, tail length, eyes shape, etc.

So, for example, you cannot create a house from the cat class, because a house must have certain characteristics — such as having a door, windows and a roof — and none of these object properties can be found in the cat class.

A class declaration is made up of the following parts:

  • Modifiers

  • Class name

  • Superclass (the name of a class’ parent, if available)

  • Interfaces

  • Keywords

  • Class body within curly brackets {}

Constructors are used to create and initialize new objects in a class. Every class must have a constructor — either a default one provided by the Java compiler or a new one written for that class.

The constructor is invoked every time a new object is built, but a class may have multiple constructors. In the “cat” class, the constructor could be an imaginary mother cat that gives birth to all the cats in the world (if she existed, I would like to pet her).

All the variables that define both the class and the objects (tail length, race, fur, etc.) are that class’ fields.

To implement the behavior of the class and its objects, methods are used. For example, “playing with a string” or “meowing to request food at 4:00 AM” are methods.

Three Different Types of Variables

Local variables

Temporary variables defined inside methods. They are declared and initialized within that method, and will be destroyed once the method is completed.

For example: in the method “playing with a string,” the string is a local variable.

Instance variables

They are variables that are inherent to an object and that can be accessed from inside any method, constructor or block.

They are destroyed when the object is destroyed.

Class variables

Class variables or static variables are declared with the static keyword in a class. They are similar to instance variables, but they are created when the program starts and destroyed when the program stops.

This definition was written in the context of Java
Share this: