Java is a fully Object-Oriented Programming language, meaning we write programs using classes and objects. Let’s dive in and explore them separately.
Class in Java
A class is a group of objects that have common properties. It is a template or blueprint by which objects are created. A class in Java can contain:
- data-members
- methods
- constructors
- blocks
- other classes
Syntax of a class declaration:
class ClassName { data member; method(); }
An example of a class:
class Vehicle { // data members/fields String brand; int maxSpeed; // constructor public Vehicle() { // body of the constructor } // method public void printMaxSpeed() { // body of the method } // instance initializer code block { System.out.println("This is a code block!"); } }
Object in Java
An object is a specific instance of a certain class. It has its own unique identity and properties, such as condition and behaviour.
The state of an object (properties, attributes) represents data fields with their current values. The behaviour of an object (actions, operations) is defined by methods.
To invoke an object’s method, the object must be asked to perform a specific action (operation). The identity of an object refers to some of its properties by which it differs from any other object.
Each object is unique because any two or more objects have an individual identity regardless of their similarities.
We can create many objects (instances of a class). Creating an instance is an instantiation of the class.
How to create an Object in Java?
We create objects using the keyword new.
class Test { public static void main(String[] args) { Test testObject = new Test(); } }
We can also create multiple objects of the same type in one line, like in the following example:
Rectangle r1=new Rectangle(), r2=new Rectangle(); //creation of two objects
When we create an object of a class, we use it to call methods and access fields of that class.
Example
class Car { int maxSpeed = 300; String brand = "Ford"; public void printBrand() { System.out.println("The brand of this car is: " + brand); } } public class ClassesAndObjectsExample { public static void main(String[] args) { Car car = new Car(); // creating an object of a Car System.out.println(car.maxSpeed); // accessing the field of the Car class car.printBrand(); // calling a method } }
Examples
Example 1:
In this example, we will create a Student class with two member data: id and a name. We will create an object of class Student using the keyword new and print the object’s values.
class Student1 { int id; //data member (instance variable) String name; //data member (instance variable) public static void main(String args[]) { Student1 s1 = new Student1(); //creating object of a class Student System.out.println(s1.id); System.out.println(s1.name); } }
Example 2:
In this example, we will create two objects of the Student class and initialize the values by calling the insertRecord() method.
We will show the state (data) of the objects by calling the displayInformation() method.
class Student2 { int id; String name; void insertRecord(int studentId, String studentName){ //method id=studentId; name=studentName; } void displayInformation() { //method System.out.println(id + " " + name); } public static void main(String args[]) { Student2 s1=new Student2(); Student2 s2=new Student2(); s1.insertRecord(111,"John"); s2.insertRecord(222,"Steve"); s1.displayInformation(); s2.displayInformation(); } }