Cosmic Module

J

Qubits of DPK

March 15, 2026

Core Java

Java Naming Conventions

javascript
QUBITS OF DPK
1Concept          Convention         Example
2────────────────────────────────────────────────────────────
3Class            PascalCase         StudentAccount, BankService
4Interface        PascalCase         Runnable, Comparable
5Method           camelCase          calculateInterest(), getName()
6Variable         camelCase          studentName, totalMarks
7Constant         UPPER_SNAKE_CASE   MAX_SIZE, PI, DB_URL
8Package          lowercase          com.deepak.banking
java
QUBITS OF DPK
1// Good naming
2class PaymentProcessor {
3    private static final double TAX_RATE = 0.18;
4    private String transactionId;
5
6    double calculateTotal(double amount) {
7        return amount + (amount * TAX_RATE);
8    }
9}

Anonymous Objects

An object created without assigning it to a reference variable. Used once and immediately garbage collected.
java
QUBITS OF DPK
1// Named object — can use multiple times
2Car myCar = new Car();
3myCar.accelerate();
4myCar.brake();
5
6// Anonymous object — used once, no reference
7new Car().accelerate();   // created, used, gone!

When to Use Anonymous Objects

java
QUBITS OF DPK
1// 1. Method argument — one time use
2printCarInfo(new Car("Toyota", 100));
3
4// 2. Chaining without storing
5new StringBuilder("Hello").append(" World").toString();
6
7// 3. Event listeners (common in Android/GUI)
8button.setOnClickListener(new ClickListener());

️ Anonymous Object Traps

java
QUBITS OF DPK
1// Trap 1 — Can't use it twice
2new Car().accelerate();
3new Car().brake();   // ⚠️ TWO different objects!
4
5// Trap 2 — Immediately eligible for GC
6new Car();    // object created but immediately unreachable
7              // GC can collect it right away

Interview Answer

"Java naming conventions: classes and interfaces use PascalCase, methods and variables use camelCase, constants use UPPER_SNAKE_CASE, packages use lowercase. An anonymous object is created with new but not assigned to a reference variable — used once and immediately eligible for garbage collection. Common use cases: passing objects directly as method arguments, one-time method calls, and event listeners where storing the reference isn't needed."

Interview Questions & MAANG-Level Answers

Q1. What are Java naming conventions?
Classes and interfaces: PascalCase (BankAccount, Runnable). Methods and variables: camelCase (calculateInterest(), studentName). Constants (static final): UPPER_SNAKE_CASE (MAX_RETRY_COUNT, DB_URL). Packages: all lowercase, reverse domain (com.google.auth). These aren't enforced by the compiler but are industry-standard — MAANG companies enforce them strictly in code reviews.
Q2. What is an anonymous object?
An object created with new without assigning it to a reference variable. It is used immediately and is eligible for garbage collection right after use:
java
QUBITS OF DPK
1new Car().accelerate();  // created, used, no reference kept
Vs named object: Car myCar = new Car(); myCar.accelerate();myCar keeps the object alive for reuse.
Q3. When would you use an anonymous object over a named reference?
When you need to use an object exactly once: (1) Passing as a method argument: printInfo(new Student("Deepak", 101)). (2) Chaining methods in one expression: new StringBuilder("Hi").append(" Deepak").toString(). (3) Event listeners/callbacks where you implement and discard: button.addListener(new ClickListener() { ... }). (4) Testing one-off scenarios. If you need the object more than once, always use a named reference.
Q4. What happens to an anonymous object after it's used?
Since no reference variable holds it, the object immediately becomes unreachable. The Garbage Collector marks it as eligible for collection and will reclaim its heap memory in the next GC cycle. This is memory-efficient for truly one-time use — no reference kept alive unnecessarily. However, in a loop, creating many anonymous objects repeatedly adds GC pressure.
Q5. What is the convention for constants in Java?
Constants are declared static final and named in UPPER_SNAKE_CASE:
java
QUBITS OF DPK
1public static final double PI = 3.14159;
2public static final int MAX_CONNECTIONS = 100;
3public static final String GATEWAY_URL = "https://api.stripe.com/v1";
Accessed via class name: MathConstants.PI. Placing related constants in an interface or enum is also common. In modern Java, prefer enums over static final int constants for type safety.