Object-Oriented Programming Overview
In the Java programming language, Object-Oriented Programming (OOP) serves as the foundational paradigm. A class acts as a blueprint or template, while an object is a concrete instance of a class. The core characteristics that define OOP are encapsulation, inheritance, and polymorphism.
Class Structure
Classes are composed of members, primarily variables (attributes) and methods (behaviors).
Access Modifiers and Scope
Access modifiers control the visibility of class members, such as public, private, and protected. Variables defined within a method body are local variables and are only accessible within that specific block.
Static Members
Members declared with the static modifier belong to the class level rather than the instance level. This means static variables and methods are shared among all objects of that class.
There are strict constraints regarding static methods:
- The
thiskeyword cannot be referenced within a static method. - Non-static (instance) methods cannot be called directly from a static method.
- Local variables inside a method cannot be declared as
static.
public class SystemConfig {
static final double PI = 3.1415;
static int instanceCount = 0;
public static void initialize() {
// Logic to initialize system
}
public void performAction() {
System.out.println(SystemConfig.PI);
System.out.println(SystemConfig.instanceCount);
SystemConfig.initialize();
}
public static SystemConfig getConfig() {
// performAction(); // Compile Error: Non-static method cannot be referenced
// return this; // Compile Error: 'this' cannot be used in static context
return null;
}
}
The this Keyword
The this keyword refers to the current instance of the class. It is primarily used to resolve naming conflicts between instance variables and parameters, or to return the current object from a method.
public class UserProfile {
private String username;
public String getDisplayInfo() {
int id = 0;
setUsername("admin");
return id + ":" + this.username;
}
public void setUsername(String username) {
this.username = username; // Assigns parameter value to the instance variable
}
public UserProfile getInstance() {
return this; // Returns the current object reference
}
}
Constructors
A constructor is a special block of code invoked when an object is instantiated. It must have the exact same name as the class and has no return type, not even void. If no constructor is explicitly defined, the compiler provides a default no-argument constructor.
Constructors can be overloaded. One constructor can call another using this(), provided it is the first statement in the block.
public class ServerConnection {
public ServerConnection() {
this("default-host"); // Calls the parameterized constructor
System.out.println("Default constructor executed");
}
public ServerConnection(String hostAddress) {
System.out.println("Constructor called with host: " + hostAddress);
}
}
Object Manipulation
Creating Objects
Objects are instantiated using the new keyword, which allocates memory and calls the constructor.
MyClass obj = new MyClass();
Accessing Properties and Behaviors
Once an object exists, its members are accessed via the dot operator (.). Each object maintains an independent state; modifying a member variable of one object does not affect others.
public class NumberProcessor {
int value = 47;
public void calculate() {
System.out.println("Calculating...");
for (value = 0; value < 3; value++) {
System.out.print(value + " ");
if (value == 2) {
System.out.println();
}
}
}
public static void main(String[] args) {
NumberProcessor p1 = new NumberProcessor();
NumberProcessor p2 = new NumberProcessor();
p2.value = 60; // Modifying only p2's state
System.out.println("Instance 1 value: " + p1.value++);
p1.calculate();
System.out.println("Instance 2 value: " + p2.value);
p2.calculate();
}
}
Object Destruction and Garbage Collection
Every object has a lifecycle. Java utilizes an automatic garbage collection mechanism to reclaim memory used by objects that are no longer reachable. The garbage collector primarily handles objects created via the new operator.
For objects that acquire resources outside of the standard Java heap (such as native memory), the finalize() method can be overridden. Defined in the Object class, finalize() is called by the garbage collector before the object is reclaimed, allowing for specific cleanup logic. However, the actual memory reclamation happens during a subsequent garbage collection cycle.