Stop 01 · Java FoundationsTopics · 7/7

Comments, Naming & Code Style

Part of the Java Roadmap

In short

Code is read far more often than it is written, by teammates, reviewers and you six months later. Good names and a few clear comments make Java code feel like a well-labelled kitchen instead of a jumbled storeroom.

Java uses // and /* */ comments plus /** */ Javadoc. By convention classes use PascalCase, methods and variables use camelCase, and constants use UPPER_SNAKE_CASE.

The jaadu sticks neat paper labels onto a row of colourful spice jars on a kitchen shelf.

Think of it like this

Labelled masala dabba

In a good kitchen, every jar is labelled: haldi, jeera, garam masala. Nobody has to open and sniff each one. Good names are those labels. Comments are the little note on the jar that says "extra spicy, use half a spoon", useful only when the label alone is not enough.

The big ideas

What you need to know

PascalCase for classes

BankAccount, OrderService, UpiPayment. Class names are nouns.

camelCase for methods and variables

totalAmount, calculateGst(), isActive. Methods usually start with a verb.

UPPER_SNAKE_CASE for constants

static final double GST_RATE = 0.18; Packages are all lowercase, like com.shop.billing.

Comment the why

Use // for short notes, /* */ for blocks and /** */ Javadoc for public classes and methods.

See it in code

Walk through the code

java
1/**2 * Calculates bills for a small shop.3 */4public class BillCalculator {5 6    static final double GST_RATE = 0.18;7 8    /**9     * Returns the price including GST.10     * @param basePrice price before tax, in rupees11     */12    static double priceWithGst(double basePrice) {13        // Round to 2 decimals so the bill shows paise correctly14        return Math.round(basePrice * (1 + GST_RATE) * 100) / 100.0;15    }16 17    public static void main(String[] args) {18        double itemPrice = 499.0;19        System.out.println(priceWithGst(itemPrice));20    }21}

1/4A Javadoc comment. Tools turn these into HTML documentation.

Javadoc on the class and method, a constant in UPPER_SNAKE_CASE, and one comment that explains a decision.

Save yourself the pain

Common mistakes

Oops

Names like a, temp2 or data everywhere.

Do this instead

Say what it holds: monthlyRent, failedLogins, customerName. Short names are fine only for loop counters like i.

Oops

Comments that repeat the code, like // add 1 to count above count++.

Do this instead

Delete them. Comment decisions, rules and surprises, such as why a value is rounded.

Your mission

Clean up a messy class

~15 min

Quick check

Which name follows Java conventions for a constant?

Go deeper

  1. 1Rename variables in an old program so each name says what it holds.
  2. 2Write a Javadoc comment for one method and generate docs with the javadoc tool.
  3. 3Turn on IntelliJ's reformat code shortcut and use it before every commit.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge