In Java, Classes are the templates which describes the behavior and state of its objects and Objects are the instances of the class which are created at the run time and has there own state and can access behavior described in the class. These definitions are the classic way of defining class and object. But when it comes to practically implement them, it requires more than knowing about the definition.
Every Java programmer starts writing code with few basic lines of the code and one of them is:
ClassA classA = new ClassA();
Which is one of the basic means to create an object of the class using new keyword. No one tries to understand what actually happens when we execute this code. On high level, yes it gives us an instance of the class. But internally it does a lot and we will explain that shortly. So lets continue learning different ways of loading a class and create objects in java.
Different ways to Load Class in Java:
A class is always loaded by the Java Classloader on demand, when the class is required by the program. But how do we define in program that we need class now? There are four different ways to do that:
Creating a reference
SomeInstance someInstance = null;
As soon as a reference is found the program the class is loaded.
Using forName method of Class class
This loads the class and initialize using Classloader of the current class.
It loads the class using SystemClassLoader or CustomClassLoader. It gets the instance of the classloader and then loads class using that instance. This method doesn’t initialize the loaded class.
Using overloaded forName method of Class class
Class.forName(String name, boolean initialize, ClassLoader loader);
Above Syntax takes the name of the class to be loaded, boolean value to decide whether to initialize loaded class’s instance or not, and what class loader to be used for class loading.
Different ways to Create Objects in Java:
Using new Keyword
MyObject myObject = new MyObject();
Creating Java object using new keyword is known to everyone who works in Java. But what not everyone knows is that by writing above line of code, we have loaded the class and initialized it at the same time. If we define it in two lines then we can break it into two different steps, one to load class and other to initialize the reference.
MyObject myObject; myObject = new MyObject();
Using forName method of Class class and newInstance method
MyObject myObject = (MyObject)Class.forName("com.talentcookie.MyObject").newInstance();
As we have read above in different ways of loading a class, this code will load the class and also create an instance of the same class.
By creating a clone of the object
MyObject myObject = new MyObject(); MyObject anotherObject = (MyObject)myObject.clone();
The clone() method can be called on an instance of the class to clone its state into another reference of the class.
De-serializing a serialized input stream (of same class’s object) into an object
ObjectInputStream inStream = new ObjectInputStream(anInputStream); MyObject object = (MyObject)inStream.readObject();
Object De-serialization is one another way to create an object. The object is actually created from the serialized form of the object available as input stream.
So we have learned different ways to load a class and creating an object. If you have any questions shoot in the comments. 🙂