Java Core Concepts: Methods, Scopes, and Object Lifecycle

Method Overloading

Method overloading allows a class to define multiple methods with the identical name but distinct parameter signatures. This improves code readability for operations that are conceptually the same but handle different data types.

public static float calculateMax(float valA, float valB) {
   return (valA > valB) ? valA : valB;
}

public static int calculateMax(int valA, int valB) {
   return (valA > valB) ? valA : valB;
}

The Java compiler selects the appropriate implementation based on the arguments provided during the call. Overloading requires unique parameter lists; varying the return type or access modiifers alone is insufficient to overload a method.

Variable Scope

The scope of a variable defines the region of the code where it can be referenced. Local variables are initialized within a method or block and persist until that block terminates. Method parameters function as local variables scoped to the entire method body. While you can reuse variable names in disjoint blocks, re-declaring a variable within a nested block that is already defined in an outer scope is prohibited.

Command-Line Arguments

Java programs can receive input at runtime via the main method's string array argument. These inputs are passed from the command line after the class name.

public class TerminalInput {
   public static void main(String[] args) {
       for (int i = 0; i < args.length; i++) {
           System.out.printf("Argument %d: %s%n", i, args[i]);
       }
   }
}

Constructors

Constructors are specialized methods invoked when an object is instantiated. They share the class name and do not specify a return type. If no constructor is explicitly defined, the compiler provides a default, parameterless constructor. Defining a custom constructor suppresses the default one.

class DataHolder {
   int value;
   DataHolder(int input) {
       value = input;
   }
}
// Usage: DataHolder obj = new DataHolder(50);

Variable Arguments (Varargs)

Introduced in JDK 1.5, variable arguments allow a method to accept a flexible number of arguments of the same type. The varargs parameter must be the final parameter in the method signature and is denoted by an ellipsis (...).

public static void computeSum(double... values) {
   double total = 0;
   for (double n : values) {
       total += n;
   }
   System.out.println("Sum: " + total);
}

The finalize() Method

The finalize() method acts as a cleanup hook that the garbage collector executes before reclaiming an object's memory. Its typically used for releasing system resources such as file handles or network connections. While the JVM manages memory automatically, finalize() provides a mechanism for explicit teardown logic.

protected void finalize() throws Throwable {
   try {
       // Cleanup resources here
   } finally {
       super.finalize();
   }
}

Tags: java object-oriented-programming JVM method-overloading memory-management

Posted on Sat, 27 Jun 2026 16:46:00 +0000 by anoopd