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 actual attributes/values 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 will 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 “Cat” 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)
-
Implemented Interfaces (if any)
-
Appropriate Keywords depending on whether the class extends from a Superclass and/or implements one or more interface
-
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. It is used to assign default values to each class’ variable to create a fully formed object. All the variables that define both the class and the objects (tail length, race, fur, etc.) are that class’ fields. In the “Cat” class, you can’t imagine a physically existing cat that has no fur, height, weight, etc. The constructor, therefore, initializes an object with a set of standardly assigned values that could be set by the programmer or by Java (default constructor).
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 made eligible for garbage collection 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.The main difference with instance variables is in what scope they are available. A class variable is accessible from an object instance, while an instance variable is not accessible from a class method.