Java Object and Classes are the fundamental components of OOP’s. Often there is confusion between classes and objects. In this tutorial, we try to tell you the difference between Class and Object in Java.
What is Java object and classes?
We know that Java is an object-oriented programming language. Classes and Objects are basic concepts of Object-Oriented Programming that revolve around real-life entities. A Class works as an object constructor, without classes. It is a “blueprint” for creating objects.
Object:
An entity that has a state and function is known as a java object. For e.g, a chair, pen, table, car, etc. A java object can be physical or logical. An object in Java is an instance of a class. A class is a blueprint from which objects are created. So, an object is the instance(result) of a class.
An object is consists of:
- State: It is represented by an object’s attributes. It also shows the properties of an object.
- Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
- Identity: It provides a unique name to an object and enables one object to interact with different objects.
Class:
A Java class is a group of objects that have common properties. Class is a blueprint from which objects are created. Unlike Objects, It is a logical entity. It can not be a physical entity.
The class can contain:
- Fields
- Methods
- Constructors
- Blocks
- Nested class and interface
We will discuss it briefly in the coming Java Tutorial series.
What is difference between java object and classes?
As we know already from the definition of java object and classes that a Java class in blueprint from where objects are created in the other hand Java object is an instance of classes. An object is a real-world and physical entity whereas a Class is a logical entity. We can create an object many times but the class should be declared only once in the whole program. Java object allocates memory when it created but Class do not
Create a Class:
To create a class, we have to use the class keyword and the class’s name should start with a capital letter. For example:
class Dog { ...
public class Student{
int stid;//field or data member or instance variable
String name;
public static void main(String args[]){
Student s=new Student();
System.out.println(s.stid);//accessing member through reference variable
System.out.println(s.name);
}
}
Output:
Create an object in java:
In Java, an object is created from a class. We have already created the class named Student
, so now we can use this to create objects.
Syntax: Classname reference Variable = new classname()
Here ‘new’ is used for creating a object.
public class Student{
int stid;//field or data member or instance variable
String name;
public static void main(String args[]){
Student s=new Student();//creating an object of Student
System.out.println(s.stid);
System.out.println(s.name);
}
}
How many ways we can create a Java Object?
There are several ways to create an object in java. They are:
1. By new keyword
2. By newInstance() method
3. By clone() method
4. By deserialization
5. By factory method etc.
We will learn all these ways to create objects in coming tutorials.
What is the meaning of the ‘this’ keyword?
In java, the ‘this’ keyword refers to the current object.
What is the use of the clone() method in java?
If a class performs the cloneable interface, then calling the clone() method on its object returns a copy of the object. If a class does not perform the cloneable interface, and the clone() method is called on its object, then the method shows a CloneNotSupportedException exception.
What is a transient keyword in the Java programming language?
The transient keyword in java is used in serialization. If you define any data member as transient, it will not be serialized.
This is all about the basic details on java objects and classes, We will discuss more on this java object and classes topic like encapsulation, methods, constructor, and more.