An object is an instance of a class. A class is a template or blueprint from which objects are created.
An object in Java is the physical as well as a logical entity, whereas, a class in Java is a logical entity only.
class in Java can contain: Fields Methods Constructors Blocks Nested class and interface
Method in Java
In Java, a method is a function defined under a class which is used to expose the behavior of an object.
Ways to initialize object
By reference variable
Student s1=new Student();
s1.id=101;
s1.name=“Sonoo”;
By method
Student s1=new Student();
s1.insertRecord(111,“Karan”);
By constructor Constructor name must be the same as its class name A Constructor must have no explicit return type A Java constructor cannot be abstract, static, final, and synchronized
constructor is called “Default Constructor” when it doesn’t have any parameter constructor which has a specific number of parameters is called a parameterized constructor.
Anonymous object
Anonymous simply means nameless. An object which has no reference is known as an anonymous object. It can be used at the time of object creation only.
If you have to use an object only once, an anonymous object is a good approach. For example:
new Calculation().fact(); //anonymous object
What is the meaning of object cloning in Java?
- Object.clone() method is used for creating an exact copy of the object in Java.
- It acts like a copy constructor. It creates and returns a copy of the object, with the same class and with all the fields having same values as of the original object.
- One disadvantage of cloning is that the return type is an Object. It has to be explicitly cast to actual type.