1. How Object is created in Java?
The object is a basic building block of an OOPs language. In Java, we cannot execute any program without creating an object.
Java provides five ways to create an object.
-Using new Keyword
-Using clone() method
-Using newInstance() method of the Class class
-Using newInstance() method of the Constructor class
-Using Deserialization
i) Using new Keyword
Using the new keyword is the most popular way to create an object or instance of the class. When we create an instance of the class by using the new keyword, it allocates memory (heap) for the newly created object and also returns the reference of that object to that memory. The new keyword is also used to create an array.
ii) Using clone() Method[TBD]
The clone() method is the method of Object class. It creates a copy of an object and returns the same copy. The JVM creates a new object when the clone() method is invoked. It copies all the content of the previously created object into new one object. Note that it does not call any constructor.
iii) Using newInstance() Method of Class class[TBD]
The newInstance() method of the Class class is also used to create an object. It calls the default constructor to create the object. It returns a newly created instance of the class represented by the object. It internally uses the newInstance() method of the Constructor class.
iv) Using newInstance() Method of Constructor class[TBD]
t is similar to the newInstance() method of the Class class. It is known as a reflective way to create objects. The method is defined in the Constructor class which is the class of java.lang.reflect package. We can also call the parameterized constructor and private constructor by using the newInstance() method.
v) Using Deserialization[TBD]
In Java, serialization is the process of converting an object into a sequence of byte-stream. The reverse process (byte-stream to object) of serialization is called deserialization. The JVM creates a new object when we serialize or deserialize an object. It does not use constructor to create an object. While using deserialization, the Serializable interface (marker interface) must be implemented in the class.
Reference: https://www.javatpoint.com/how-to-create-object-in-java
2. How method is called in Java?
In Java, a method is a block of code that performs a specific function and runs only when it is called. Methods are also commonly known as functions. Each method has its name. You can pass data into a method via parameters. A method also has a return type defining the type of data it returns. According to the convention, the name of the method should be written in lowerCamelCase where the first letter should be small. Furthermore, a method should have a proper name, preferably a verb referring to what it does.
Reference:https://codegym.cc/groups/posts/how-to-call-a-method-in-java-
3. How method is defined in Java?
Top comments (0)