This tutorial guides you on what is a class and how Java deals with objects. You will know how to create a class and instantiate its object.
Let’s now begin to understand the basic building blocks of object-oriented programming (OOP) in Java.
Basics of Classes and Objects in Java
You can go through the following sections to learn about Java Class.
Object-Oriented Programming
Many a time you must have come across the phrase Java is an Object-Oriented Programming Language. The term Object-Oriented denotes a concept in software development.
It is a way of organizing software in the form of objects that contain both data and the behavior of these objects. Therefore, Object-Oriented Programming Languages, generally known as OOP, provide the programming model for simplifying software development, design, and maintenance under some well-established ground rules.
The projects made in OOPS are more structured towards objects. As a result, it increases the performance, maintainability, and development of the program. The main idea behind OOP is to incorporate data and behavior under the same location(objects).
The fundamental concepts supported by OOPS are:
- Classes
- Objects
- Polymorphism
- Inheritance
- Encapsulation
- Abstraction
Class in Java
A class in Java or any other Object-oriented Language is a blueprint for objects to follow a specific schema defined in the class.
Classes define the behavior of objects of their type. It represents a collection of properties (data and functions) for all its objects.
It supports a template for creating objects that bind code and data. Further, classes act as a means to define methods and data. It helps in maintaining access specifications for member variables using access specifiers.
Object in Java
An Object is the most fundamental entity in Java or any other Object-Oriented Language. Objects represent real-life entities because each of them could have specific behavior, identity, and data (attributes).
In Java, the object is an offspring of the class. The class has properties to reflect the object state and methods to represent the behavior.
The methods also show an object’s response to other objects. Identity is a unique name for the object assigned by the user, much like variables.
Let’s have a profound look into what are objects. If we think about this present reality, we can discover numerous articles around us, vehicles, people, and so on. Every one of these has a unique state and behavior. You can’t expect a human being to bark like a dog or a dog to speak like a human.
For example – A car, its state, name, model no, shade, manufacturer, and its behavior can be – moving, blinking the headlights, honking, etc.
If you try to compare a Java object with any real-time entity, they could probably have fundamentally the same attributes.
Must Read – Java Multithreading
Java Class – Example
public class Car { // Class Attributes - State of an object String color; int model_no; String name; String manf; // Class Methods - Behaviour of an object void honk() { } void move() { } void blink() { } }
A class can have any number of functions to access the properties of the class’s object or manipulate the properties. In the above example, move(), blink(), and honk() are a few methods.
Also Read: Constructor in Java
Variable Types
A class can contain any of the accompanying variable sorts:
Class variables:
A class variable is one that has the static keyword as a prefix in its declaration. Its definition occurs only inside a class and outside any function.
Local variables:
These are variables that have declarations inside methods, constructors, or blocks. They are local to the part of the code they belong.
Local variables come into existence when the control enters into the code block that keeps their declaration. And they vanish with the block leaving out of execution.
Instance variables:
These variables are inside a class however outside any method. They come into existence when the class instantiates. These are accessible from any constructor or block of that specific class.
Also Check: Inheritance in Java