Deep Dive into Java Methods: Return Values, Getters/Setters, static & final
This tutorial walks through Java member methods, covering return statement syntax, proper use of getters and setters, the static modifier for class members, and the final keyword for classes, variables, and methods, with concrete code examples and explanations.
1. Return values in member methods
If a member method declares a return type, the method body must contain a return statement that provides a value of the same type. Two equivalent syntaxes are shown:
return expression return(expression)When the declared return type is void, no return statement is required. A mismatch between the declared type and the returned expression causes a compilation error, e.g., a method declared to return String but returning a numeric literal 15.6 will not compile.
Methods can also return complex types such as objects. The following example returns a newly created Point object:
public Point getPoint(int num1, int num2) {
return new Point(num1, num2);
}Methods exit and return to the caller when (1) all statements have executed, (2) a return statement is reached, or (3) an exception is thrown.
2. get() and set() methods
Getter and setter methods are basic Java methods used to access private fields. By convention, the method name starts with get or set followed by the capitalized field name, e.g., getBrand() or setSpeed(double newSpeed). Getters retrieve a field value; setters assign a new value.
Example of a Car class with getters for brand, color, and speed, and a setter for speed:
public class Car {
private String brand; // car brand
private String color; // car color
private double speed; // car speed
public String getBrand() { return brand; }
public String getColor() { return color; }
public double getSpeed() { return speed; }
public void setSpeed(double newSpeed) { speed = newSpeed; }
}When only getters are defined, the object is read‑only; when both getters and setters exist, it is read‑write; defining only a setter creates a write‑only property.
A test program demonstrates usage:
class CarTest {
public static void main(String[] args) {
Car car = new Car();
System.out.println("Brand:" + car.getBrand());
System.out.println("Color:" + car.getColor());
System.out.println("Speed:" + car.getSpeed());
}
}Running this code prints Brand:null, Color:null, and Speed:0.0 because the fields have not been initialized.
3. static modifier
The static keyword can decorate member methods and member variables. A static method (also called a class method) belongs to the class itself and is invoked as ClassName.methodName(...). A static variable (class variable) is shared by all instances of the class.
Example of a static variable that counts how many Num objects have been created:
class Num {
public static int num = 0;
Num() { num++; }
}
public class StaticVariableTest {
public static void main(String[] args) {
System.out.println("Before first object, num=" + Num.num);
Num n1 = new Num();
System.out.println("Before second object, num=" + Num.num);
Num n2 = new Num();
System.out.println("After second object, num=" + Num.num);
}
}Output shows the counter incrementing with each new instance, demonstrating that static variables are shared across objects.
4. Class members vs. instance members
When a class is loaded, it occupies a memory address that can be referenced by the class name or any of its objects. Instance methods require an object reference to be invoked, while class (static) methods can be called directly via the class name. Class members can be accessed through both the class name and an object reference, but using the class name is recommended for clarity. The this and super keywords cannot reference static members because they refer to a specific object instance.
5. final modifier
The final keyword can be applied to classes, variables, and methods.
final class : cannot be subclassed.
final variable : becomes a constant; its value cannot be changed after initialization.
final method : cannot be overridden by subclasses.
These rules help enforce immutability and prevent unintended inheritance or modification.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
