Hey guys in this article, you will understand the difference between Class and Object in Java with easy to understand examples
Read More:
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
- Check the Spring Boot JdbcTemplate Tutorials
Class vs Object
Following are the difference between Class and Object
Object | Class |
Object is an instance of a class. | Class is a blueprint or template from which objects are created. |
Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc. | Class is a group of similar objects. |
Object is a physical entity. | Class is a logical entity. |
Object is created through new keyword mainly e.g. Student s1=new Student(); |
Class is declared using class keyword e.g. class Student{} |
Object is created many times as per requirement. | Class is declared once. |
Object allocates memory when it is created. | Class doesn’t allocated memory when it is created. |
There are many ways to create object like new keyword, newInstance() method, clone() method, factory method & deserialization. |
There is only one way to define class in java using class keyword. |
Class – Example
Following is the example for a Employee class
public class Employee {
private int eid;
private String ename;
private int eage;
public String getEmployeeName() {
return ename;
}
}
Object – Example
Following is the example for creating an object for Employee class
Employee e = new Employee();
That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.