- Different Methodologies in Project Management
- Difference between Soap and Rest | Know more about it
- What is Python array? Learn with examples
- Best Career options after Engineering | Everything You Need to Know [OverView]
- What is list in Python ? All you need to know [ OverView ]
- What is Data Modelling? : All you need to know [ OverView ]
- What are Microservices? : A Complete Guide For Beginners with Best Practices
- What is Python Programming | A Definitive Guide with Best Practices
- All You Need To Know About Python List | A Complete Guide For Beginners with Best Practices
- What Is a Software Developer | Software Developer job description and duties | Everything You Need to Know
- What is Artificial Intelligence Chatbot?
- Kotlin vs Java | Know Their Differences and Which Should You Learn?
- What is Abstraction in Java | Implementations of Abstraction in Java | A Definitive Guide with Best Practices
- What are the Important Data Structures and Algorithms in Python?
- Go vs Python | Know Their Differences and Which Should You Learn?
- Best Python IDEs and Code Editors | Expert’s Top Picks | Everything You Need to Know
- Go Programming Language | Expert’s Top Picks | A Definitive Guide [ OverView ]
- Python Scopes and Their Built-in Functions | Everything You Need to Know | Expert’s Top Picks
- Python String Formatting | A Complete Guide For Beginners [ OverView ]
- Python Serialization | A Complete Guide For Beginners | Learning Guide
- What is .Net FrameWork? Uses and its Benefits | Everything You Need to Know
- What is Quality Assurance ? : A Definitive Guide | Everything You Need to Know [ OverView ]
- What is Spike Testing ? : A Definitive Guide | Expert’s Top Picks | Free Guide Tutorial
- Average Full Stack Developer Salary in India [ For Freshers and Experience ]
- What is WSDL in Web Services ? Expert’s Top Picks | Free Guide Tutorial
- Virtual Instrumentation using Labview | Comprehensive Guide [ Explained ]
- Gradle vs Maven | Know Their Differences and Which Should You Learn?
- Python Sleep Method | Free Guide Tutorial & REAL-TIME Examples
- Kotlin vs Python | A Complete Guide with Best Practices
- Spring Boot vs Spring MVC | Differences and Which Should You Learn?
- IT Engineer Salary in India – How much does one earn?
- What is pip ? and Getting Started with Python pip | All you need to know [ OverView ]
- Node.js Installation on Windows and Ubuntu | Free Guide Tutorial
- Skills Needed for Full Stack Developers | All you need to know [ OverView ]
- What is Axios in React? and Its Uses [ OverView ]
- What is MEAN Stack? All you need to know [ OverView ]
- How to Install Node.JS on Ubuntu | Everything You Need to Know
- Average Annual Salary of a Python Certified Professional – Career Path
- What is Scala Programming? A Complete Guide with Best Practices
- What Is User Input in Python? Expert’s Top Picks
- Interface vs Abstract Class | Difference You Should Know
- Final Year Computer Science Project Ideas | All you need to know [ OverView ]
- Technical Architect | Free Guide Tutorial & REAL-TIME Examples
- Logical Programs in Java | Step-By-Step Process
- C++ vs Java | Difference You Should Know
- What is C Programming? Comprehensive Guide
- What Is a Quality Engineer? ( Everything You Need to Know )
- Python Project Ideas for Beginners | All you need to know
- How to Run Python Scripts? Comprehensive Guide
- Python Operators
- How To Install NumPy in Python?
- Top Software Courses to Get High Paying Jobs
- Loops In Python
- Tips to Avoid Application Rejection
- Top Young App Developers Who Became Millionaires
- Top Technical Courses After Graduation
- Node JS Architecture
- What is PyCharm?
- Resources To Help You Learn Java Programming
- How to Become a Software Engineer?
- Best Programming Languages to Learn in 2020
- Scala vs Python
- How to Become a Full Stack Developer?
- The Most Popular Java Applications Used World-wide
- What is Java String?
- Full Stack Developer vs Front End Developer vs Back End Developer
- Python Collections
- Identifiers in Python
- Dynamic Method Dispatch in Java
- Hadoop Ecosystem
- Method Overloading in Python
- Convert Decimal To Binary In Python
- How To Make A Chatbot In Python?
- How to Input a List in Python?
- Hash Tables and Hashmaps in Python
- Top Python Framework’s
- Python Split Method with Example
- Python Enumerate
- Inheritance in Java
- Init in Python
- Goto Statement in Python
- Literals in Java
- Polymorphism in Oops
- Socket Programming in Python
- Object Class in Java
- Break, Continue, and Pass Statements in Python
- Exception Handling in Java
- Java BASIC Programs
- Different Methodologies in Project Management
- Difference between Soap and Rest | Know more about it
- What is Python array? Learn with examples
- Best Career options after Engineering | Everything You Need to Know [OverView]
- What is list in Python ? All you need to know [ OverView ]
- What is Data Modelling? : All you need to know [ OverView ]
- What are Microservices? : A Complete Guide For Beginners with Best Practices
- What is Python Programming | A Definitive Guide with Best Practices
- All You Need To Know About Python List | A Complete Guide For Beginners with Best Practices
- What Is a Software Developer | Software Developer job description and duties | Everything You Need to Know
- What is Artificial Intelligence Chatbot?
- Kotlin vs Java | Know Their Differences and Which Should You Learn?
- What is Abstraction in Java | Implementations of Abstraction in Java | A Definitive Guide with Best Practices
- What are the Important Data Structures and Algorithms in Python?
- Go vs Python | Know Their Differences and Which Should You Learn?
- Best Python IDEs and Code Editors | Expert’s Top Picks | Everything You Need to Know
- Go Programming Language | Expert’s Top Picks | A Definitive Guide [ OverView ]
- Python Scopes and Their Built-in Functions | Everything You Need to Know | Expert’s Top Picks
- Python String Formatting | A Complete Guide For Beginners [ OverView ]
- Python Serialization | A Complete Guide For Beginners | Learning Guide
- What is .Net FrameWork? Uses and its Benefits | Everything You Need to Know
- What is Quality Assurance ? : A Definitive Guide | Everything You Need to Know [ OverView ]
- What is Spike Testing ? : A Definitive Guide | Expert’s Top Picks | Free Guide Tutorial
- Average Full Stack Developer Salary in India [ For Freshers and Experience ]
- What is WSDL in Web Services ? Expert’s Top Picks | Free Guide Tutorial
- Virtual Instrumentation using Labview | Comprehensive Guide [ Explained ]
- Gradle vs Maven | Know Their Differences and Which Should You Learn?
- Python Sleep Method | Free Guide Tutorial & REAL-TIME Examples
- Kotlin vs Python | A Complete Guide with Best Practices
- Spring Boot vs Spring MVC | Differences and Which Should You Learn?
- IT Engineer Salary in India – How much does one earn?
- What is pip ? and Getting Started with Python pip | All you need to know [ OverView ]
- Node.js Installation on Windows and Ubuntu | Free Guide Tutorial
- Skills Needed for Full Stack Developers | All you need to know [ OverView ]
- What is Axios in React? and Its Uses [ OverView ]
- What is MEAN Stack? All you need to know [ OverView ]
- How to Install Node.JS on Ubuntu | Everything You Need to Know
- Average Annual Salary of a Python Certified Professional – Career Path
- What is Scala Programming? A Complete Guide with Best Practices
- What Is User Input in Python? Expert’s Top Picks
- Interface vs Abstract Class | Difference You Should Know
- Final Year Computer Science Project Ideas | All you need to know [ OverView ]
- Technical Architect | Free Guide Tutorial & REAL-TIME Examples
- Logical Programs in Java | Step-By-Step Process
- C++ vs Java | Difference You Should Know
- What is C Programming? Comprehensive Guide
- What Is a Quality Engineer? ( Everything You Need to Know )
- Python Project Ideas for Beginners | All you need to know
- How to Run Python Scripts? Comprehensive Guide
- Python Operators
- How To Install NumPy in Python?
- Top Software Courses to Get High Paying Jobs
- Loops In Python
- Tips to Avoid Application Rejection
- Top Young App Developers Who Became Millionaires
- Top Technical Courses After Graduation
- Node JS Architecture
- What is PyCharm?
- Resources To Help You Learn Java Programming
- How to Become a Software Engineer?
- Best Programming Languages to Learn in 2020
- Scala vs Python
- How to Become a Full Stack Developer?
- The Most Popular Java Applications Used World-wide
- What is Java String?
- Full Stack Developer vs Front End Developer vs Back End Developer
- Python Collections
- Identifiers in Python
- Dynamic Method Dispatch in Java
- Hadoop Ecosystem
- Method Overloading in Python
- Convert Decimal To Binary In Python
- How To Make A Chatbot In Python?
- How to Input a List in Python?
- Hash Tables and Hashmaps in Python
- Top Python Framework’s
- Python Split Method with Example
- Python Enumerate
- Inheritance in Java
- Init in Python
- Goto Statement in Python
- Literals in Java
- Polymorphism in Oops
- Socket Programming in Python
- Object Class in Java
- Break, Continue, and Pass Statements in Python
- Exception Handling in Java
- Java BASIC Programs

Object Class in Java
Last updated on 21st Sep 2020, Artciles, Blog, Software Engineering
- Object is an instance of a class while class is a blueprint of an object. An object represents the class and consists of properties and behavior.
- Properties refer to the fields declared within class and behavior represents to the methods available in the class.
- In the real world, we can understand an object as a cell phone that has its properties like: name, cost, color etc and behavior like calling, chatting etc.
- So we can say that an object is a real world entity.
- Some real world objects are: ball, fan, car etc.

Subscribe For Free Demo
Error: Contact form not found.
Java Object Syntax
- className variable_name = new className();
- Here, className is the name of a class that can be anything like: Student that we declared in the above example.
- variable_name is the name of the reference variable that is used to hold the reference of the created object.
- The new is a keyword which is used to allocate memory for the object.
Example
Lets see an example to create an object of class Student that we created in the above class section.
Although there are many other ways by which we can create objects of the class. We have covered this section in detail in separate topics.
Example: Object creation
- Student std = new Student();
- Here, std is an object that represents the class Student during runtime.
- The new keyword creates an actual physical copy of the object and assigns it to the std variable. It will have physical existence and get memory in the heap area.
- The new operator dynamically allocates memory for an object.

In this image, we can get an idea how an object refers to the memory area.
Example: Creating a Class and its object
-
public class Student
{
String name;
int rollno;
int age;
void info()
{
System.out.println(“Name: “+name);
System.out.println(“Roll Number: “+rollno);
System.out.println(“Age: “+age);
}
public static void main(String[] args) {
Student student = new Student();// Accessing and property value
student.name = “Ramesh”;
student.rollno = 253;
student.age = 25;
student.info(); // Calling method
}}
Output
Name: Ramesh
Roll Number: 253
Age: 25
In this example, we created a class Student and an object. Here you may be surprised to see the main() method but don’t worry it is just an entry point of the program by which JVM starts execution.
Here, we used the main method to create objects of the Student class and access its fields and methods.
Example: Class fields
public class Student{
String name;
int rollno;
int age;
void info()
{
System.out.println(“Name: “+name);
System.out.println(“Roll Number: “+rollno);
System.out.println(“Age: “+age);
}
public static void main(String[] args) {
Student student = new Student();// Calling method
student.info();
}}
Output
Name: null
Roll Number: 0
Age: 0
In case, if we don’t initialized values of class fields then they are initialized with their default values.
Number of ways to create an object in Java
- Using new keyword: It is the simplest, common and regular way to create an object in Java. By using this object creation method we can call any default or parameterized constructor. This object creation way is required when you are passing values to the parameterized constructor at the time of an object creation.
Example:
Student stud1 = new Student(11, “Rajveer”);
In the above example stud1 is a reference variable that refer to the created object, new keyword is responsible to allocate memory to the object. In the statement a 2 parameterized constructor will get called.
2. Using newInstance() method of Class class: The Class class has a newInstance() method to create an object of a class. This newInstance() method calls the default or no-argument constructor to create the Java object.
We can create an Java object by newInstance() method like below
Example:
Student stud = (Student)Class.forName(“Student”).newInstance();
Or:
Student stud = Student.class.newInstance();
3. Using newInstance() method of Constructor class: The java.lang.reflect. The Constructor class has a newInstance() method to create an java object. In following way we can use newInstance() method of Constructor class to create an object.
- Constructor
constructor= Student.class.getConstructor(); - Student stud = constructor.newInstance();
The above two ways are reflective ways to create an java object using newInstance() method. The newInstance() method of the Class class internally uses the newInstance() method of the Constructor class. The newInstance() method of Constructor class is preferred and used by different frameworks like Spring, Hibernate, Struts, etc.
4. Using clone() method: The Object class has a protected clone() method, if we call clone() on any object, the JVM internally creates a new java object and copies all content of the existing object into new one. This way of creating an object using the clone() method does not invoke any constructor.
To use clone() method on an object we need to implement Cloneable interface and override the clone() method into that class. If we call clone() method on a object and that class is not implementing the Cloneable interface then we will get CloneNotSupportedException.
Student stud = (Student) oldstud.clone();
5. Using deserialization: Whenever we serialize and deserialize an object, the JVM creates a separate object. In deserialization, the JVM doesn’t use any constructor to create the object.
To serialize and deserialize an object we need to implement the Serializable interface in our class.
- ObjectInputStream in = new ObjectInputStream(new FileInputStream(“sample.txt”));
- Student stud = (Student) in.readObject();