Sitemap

Java Day-24

4 min readNov 5, 2025

--

1. Method Return Types in Java

Press enter or click to view image in full size

In Java, every method must declare a return type. This specifies what kind of data the method will send back when it’s called.

There are three main categories of return types:

void: The method does not return any value.

Java

public void printMessage() { System.out.println("Hello"); // No 'return' statement with a value is needed }

Primitive Data Type: The method must return a value of that specific primitive type (like int, double, boolean, etc.).

  1. Reference Data Type: The method must return an object (or null) that is compatible with the declared class or interface.

The Golden Rule of Return Types: The value return must be compatible with the method's declared return type. A simple test is: "Could I assign the return value to a variable of that type?"

Examples:

  • Case 1: Invalid (Type Mismatch)
  • Java

public int accept() { return 12.90; // ERROR // Test: int x = 12.90; <-- This is not valid. }

Case 2: Valid (Primitive)

  • Java

public int accept() { return 12; // VALID // Test: int x = 12; <-- This is valid. }

Case 3: Valid (Reference Type / Object)

  • Java

public class Test { public Test accept() { return new Test(); // VALID: Returning a new object of type Test // Test: Test t1 = new Test(); <-- This is valid. } public Test getSelf() { return this; // VALID: 'this' refers to the current object of type Test } public Test getNothing() { return null; // VALID: 'null' is compatible with any object type } }

2. What is a Factory Method?

A factory method is a method whose return type is a class or an interface.

Its main purpose is to create and return an object, hiding the actual creation logic (the new keyword and constructor call) from the code that's calling it.

  • A factory method can be either static or non-static.
  • It’s a very common and powerful design pattern.

Key Pattern: private Constructor + public static Factory

A common way to use factory methods is to make the class’s constructor private. This prevents anyone from creating an object using the new keyword directly. The only way to get an object is to call the public static factory method.

This gives the class complete control over how and when objects are created.

Example 1: Simple Static Factory

Get Aman Verma’s stories in your inbox

Join Medium for free to get updates from this writer.

Java

// File: Book.java
package com.ravi.factory_method;
public class Book {
private String title;
private String author;
// 1. Constructor is PRIVATE
private Book(String title, String author) {
this.title = title;
this.author = author;
}
// 2. Public Static Factory Method
// This is now the ONLY way to get a Book object
public static Book getBookObject() {
// We can call the private constructor from inside the class
return new Book("Java", "James Gosling");
}
@Override
public String toString() {
return "Book [title=" + title + ", author=" + author + "]";
}
}
// File: BookDemo.java
package com.ravi.factory_method;
public class BookDemo {
public static void main(String[] args) {
// Book b1 = new Book(...); // ERROR! Constructor is private.

// You MUST use the factory method
Book b1 = Book.getBookObject();
System.out.println(b1);
}
}

Example 2: Parameterized Static Factory

The factory method can also accept parameters to create different objects.

Java

// File: Product.java
package com.ravi.factory_method;
public class Product {
private int id;
private String name;
private double price;
// 1. Constructor is PRIVATE
private Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
// 2. Public Static Factory Method that takes parameters
public static Product getProductObject(int id, String name, double price) {
// You could add validation logic here before creating the object
return new Product(id, name, price);
}

// ... toString() method ...
}
// File: ProductDemo.java
// ... (imports) ...
public class ProductDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter the product Id: ");
int id = Integer.parseInt(sc.nextLine());
// ... (get name and price) ...
// 3. Call the factory to get the object
Product p1 = Product.getProductObject(id, name, price);
System.out.println(p1);
}
}

3. What is a Shallow Copy?

A Shallow Copy is not a copy of an object at all. It is a copy of a reference variable.

This means you create only one object in memory, but you have multiple reference variables pointing to that single object.

Consequence: If you modify the object using any of its references, the change is visible through all references, because they all point to the same object.

Shallow Copy Program Example:

Java

// File: Laptop.java
package com.ravi.shallow_copy;
public class Laptop {
private String brand;
private double price;
public Laptop(String brand, double price) {
this.brand = brand;
this.price = price;
}
// Getters
public String getBrand() { return brand; }
public double getPrice() { return price; }
// Setters
public void setBrand(String brand) { this.brand = brand; }
public void setPrice(double price) { this.price = price; }
@Override
public String toString() {
return "Laptop [brand=" + brand + ", price=" + price + "]";
}
}
// File: ShallowCopyDemo.java
package com.ravi.shallow_copy;
public class ShallowCopyDemo {
public static void main(String[] args) {
// 1. One Laptop object is created in memory (e.g., at address 1000x)
Laptop lap1 = new Laptop("HP", 90000);
System.out.println(lap1); // Prints [HP, 90000]
// 2. This is the shallow copy step.
// NO new object is created.
// 'lap2' simply gets a copy of 'lap1's reference (1000x).
Laptop lap2 = lap1;
// 3. Modify the *single* object using the 'lap2' reference
lap2.setBrand("Dell");
lap2.setPrice(95000);
// 4. Print *both* references
System.out.println("After modification!!");

// This prints the modified data
System.out.println(lap1); // Output: Laptop [brand=Dell, price=95000.0]

// This prints the *same* modified data
System.out.println(lap2); // Output: Laptop [brand=Dell, price=95000.0]
}
}

--

--

Aman Verma
Aman Verma

Written by Aman Verma

📚#Software Engineer || Ai Technolgy || #ReadingIsLife || #techylife https://blog-project-one-silk.vercel.app/