OOP approach in Java
Hazel T Chikara
Thunderbird School of Global Management | Fintech | Data Science & AI Strategy | APIs | Project Management
OOP approach in Java: Object Instantiation and Destruction
Now that we have understood that classes as much as they are an anchor to OOP, they require constructors for to create instances and the instances should be initialized - initialization being giving default values. These constructors can be overloaded, meaning that the instances are at free will to have their specific implementation and can have different argument quantities and or different argument types. Note that when reusing the constructor name, the compiler differentiate using argument type and size, if you try to create a constructor with the same argument size and type it will give a compile time error.
In this section, we will break down how to instantiate (creating instances) objects. Before that, there is another form of initialization that should be known, which is an initializer block. An initializer block is a block of code that executes when an instance of an object is created. It has no data type, no associated name as you only require to place code in curly braces, and has to be outside of any method. It is also called before the constructor is called.
class DriverlessRoute {
String departureLocation;
{
this.departureLocation = "";
System.out.println(" This is a Departure Location Initializer")
}
DriverlessRoute(String id)
{
this.id = id;
this.id = UUID.randomUUID().toString();
}
}
There are two types of initializer blocks, static (initializes static fields) and non-static (initializes instance fields). Non-static initializers work similarly as constructors as they both initializes (give default values) to instance fields.
Instantiation for any object has three universal steps, for example, when creating a new driverlessRoute for an autonomous vehicle.
DriverlessRoute harareBeitbridge = new DriverlessRoute("Harare", "BeitBridge");
Step 1: We declare the driverlessRoute variable and call it HarareBeitbridge to reference the new object of driverlessRoute type.
DriverlessRoute harareBeitbridge ...
This on its own will have zero effect in the application, it will just act as a placeholder.
Step 2: Instert the "new" keyword.
... new ...
This will allocate memory to the harareBeitbridge object of type driverlessRoute and returns a reference.
Step 3: Constructor Invocation
... DriverlessRoute("Harare", "BeitBridge");
The constructor is called with the default arguments.
领英推荐
The constructor will initializes the new route object setting the default argument values,In an exmple where the arguments are the departureLocation and destinationLocation, it will set the default departureLocation to Harare and destinationLocation to Beitbridge.
We can have multiple variables to the same reference for example:
DriverlessRoute route1 = harareBeitbridge;
Instead of using a constructor for object creation, we can just assign the new variable to point to the same reference.
OBJECT DESTRUCTION
To destroy objects in Java we use garbage collection. This process searches for unused objects and automatically deletes them to free up memory. One can customize implementation to suit application needs noting that we cannot determine its intervals and which objects it should delete. Garbage Collection has serveral implementation, it goes deeper but the main procedure usually adhere to the following phases:
Mark Phase: Identification of the unused and used objects.
This process starts from special objects which are always reachable aka GC Roots.These include classes, active Java threads and local variables which are kept alive the the thread stack. From these routes, it checks the reachable objects noting the referenced and non-referenced.
Delete Phase: Deletes the non-referenced objects to free up memory,
Compact Phase: Regrouping the remaining referenced objects for performance reasons.
We have introduced OOP and how to create, initialize and destroy objects. The next series we will break down a bit more on variables and methods. This will guide us on pragmatically understanding the objects' state and behavior in OOP.
There are other best practises that will help standardize code organization. We group classes related to each other in the same "folder" called packages. This grouping is dependent on the architecture you use. Usually these classes should also follow SOLID principles to help gruanualize the classes for elasticity, simplicity and standardization.