DEV Community

Neelakandan R
Neelakandan R

Posted on

Today class---26/12/2024 Create class and object

CLASS IN JAVA:

It is an logical entity and also know as Template or Blueprint.

Classes are abstract concepts that represent the definition of an entity, not its physical existence.

Classes do not occupy memory until an object is created from the class.

without class we cannot create object.but the reverse is not true

*Eg- Car: A class that defines properties like color, model, and speed

Reference- https://www.geeksforgeeks.org/classes-objects-java/

OBJECT:

IT is an real time entity and it also physical entity

object represents class and object instance of class

 1.State : It is represented by attributes of an object. It also reflects the properties of an object.
2.Behavior : It is represented by the methods of an object. It also reflects the response of an object with other objects.
3.Identity : It gives a unique name to an object and enables one object to interact with other objects. 
Enter fullscreen mode Exit fullscreen mode

Reference:https://www.geeksforgeeks.org/classes-objects-java/

Methods to create object in java :

There are many different ways to create objects in Java. .

1.Using new keyword
2.Using new instance(TBD)
3.Using clone() method(TBD)
4.Using deserialization(TBD)
5.Using newInstance() method of Constructor class(TBD)
Enter fullscreen mode Exit fullscreen mode

Method 1: Using new keyword

Using the new keyword in java is the most basic way to create an object. This is the most common way to create an object in java. Almost 99% of objects are created in this way. By using this method we can call any constructor we want to call (no argument or parameterized constructors).

Method 2: Using new instance(TBD)

If we know the name of the class & if it has a public default constructor we can create an object Class.forName. We can use it to create the Object of a Class. Class.forName actually loads the Class in Java but doesn’t create any Object. To create an Object of the Class you have to use the new Instance Method of the Class.

Method 3: Using clone() method(TBD)

Whenever clone() is called on any object, the JVM actually creates a new object and copies all content of the previous object into it. Creating an object using the clone method does not invoke any constructor. In order to use the clone() method on an object we need to implement Cloneable and define the clone() method in it.

Method 4: Using deserialization(TBD)

Whenever we serialize and then deserialize an object, JVM creates a separate object. In deserialization, JVM doesn’t use any constructor to create the object. To deserialize an object we need to implement the Serializable interface in the class.

Method 5: Using newInstance() method of the constructor class(TBD)

This is similar to the newInstance() method of a class. There is one newInstance() method in the java.lang.reflect.Constructor class which we can use to create objects. It can also call the parameterized constructor, and private constructor by using this newInstance() method. Both newInstance() methods are known as reflective ways to create objects. In fact newInstance() method of Class internally uses newInstance() method of Constructor class.

Reference---https://www.geeksforgeeks.org/different-ways-create-objects-java/

METHOD:

Set of instruction with a name , for achieving a specific task and method calling is not important in program

How Method is called in Java:

In Java, the method is a collection of statements that performs a specific task or operation. It is widely used because it provides reusability of code means that write once and use it many times. It also provides easy modification. Each method has its own name by which it is called. When the compiler reads the method name, the method is called and performs the specified task. In this section, we will learn how to call pre-defined, user-defined, static, and abstract methods in Java.

Reference---https://www.javatpoint.com/how-to-call-a-method-in-java

Example:

public class Mobile
{
public static void main(String[] args)
{
Mobile whatsapp= new Mobile(); //object creation//new allocates memory
whatsapp.openchat(); //method calling statemnt
Mobile youtube=new Mobile();
//object creation //new allocates memory
youtube.watchvedio(); //method calling statemnt
}
public void openchat() //method signature
{
System.out.println("open chat and type hi"); //method defination//body
}
public void watchvedio() //method signature
{
System.out.println("open and watch vedio in youtube"); //method defination//body
}
}

Top comments (0)