Master clean code practices in Java with proper naming conventions, structured program design, and modern annotation based configuration.

Lilavati MhaskeLilavati Mhaske
4 min read

Writing clean, consistent, and professional Java code involves more than solving logic problems. It’s about how you name things, structure your files, and configure your applications. In this blog, we’ll cover three essential fundamentals every Java developer must know:

  • Java Naming Conventions

  • Java Program Structure

  • Java Annotations (and why they replaced XML)

Whether you're just starting Java or want a refresher with examples, you're in the right place!


Java Naming Conventions

Java is case-sensitive, meaning ABC and abc are different. To maintain clarity and consistency, Java follows well-established naming conventions.

1. Class & Interface Names

  • Start with an uppercase letter.

  • Follow CamelCase for multi-word names.

  • Represent entities, usually nouns.

Examples:

class Student {}
interface DataService {}
class InputStreamReader {}

2. Variable Names

  • Start with a lowercase letter.

  • Use camelCase for readability.

  • Should describe the variable’s purpose.

Examples:

int age;
String studentName;
boolean isEnrolled;

3. Method Names

  • Start with a lowercase letter.

  • Use verbs or action phrases.

  • camelCase format applies for multi-word names.

Examples:

void calculateBill() {}
String getUserEmail() {}

4. Constant Variables

  • Use UPPERCASE letters.

  • Separate words with underscores.

Examples:

static final int MAX_RETRIES = 3;
static final String DEFAULT_LANGUAGE = "EN";

5. Package Names

  • Must be in lowercase.

  • Use reverse domain naming convention for uniqueness.

Examples:

package com.javabrew.utils;
package org.example.myapp.service;

Note: These conventions are mandatory for Java’s built-in libraries and highly recommended for your own code too.


Java Program Structure

A standard Java program typically follows this order:

SectionRequired?Description
Comment SectionOptionalDescribes the file’s purpose or logic
Package DeclarationOptionalDeclares the package the file belongs to
Import SectionOptionalImports classes and interfaces
Class/InterfaceOptionalContains your logic or data models
Main Class✅ YesContains the main() method for execution

1. Comment Section

Java supports 3 types of comments:

// Single-line

/* Multi-line 
   comment */

/** Documentation comment */

2. Package Declaration

Packages are used to group related classes and interfaces.

Syntax:

package com.example.myapp;

Rules:

  • Only one package declaration per file

  • Must be the first non-comment statement

  • Package name must be unique and non-duplicate


3. Import Section

Used to bring external classes into your Java file.

Syntax:

import java.util.*; // imports all
import java.util.ArrayList; // specific class

You can use multiple imports, but only one package declaration per file.


4. Class/Interface Section

Defines data models or behaviors using classes or interfaces.

public class Student {
    String name;
    int age;
}
  • You can define multiple classes in a single file

  • Only one public class matching the file name is allowed


5. Main Class Section

This class includes the main() method and serves as the entry point.

Example:

public class MainApp {
    public static void main(String[] args) {
        System.out.println("Welcome to Java!");
    }
}

What are Annotations in Java?

Annotations are metadata added to your code to instruct the compiler or runtime how to behave. Introduced in JDK 1.5, they’ve become a core part of modern Java programming.

Examples:

@Override
public String toString() {
    return "Student";
}

@Deprecated
public void oldMethod() {}

In modern frameworks like Spring Boot, you’ll see annotations like @Service, @Autowired, or @RestController.


Why Did Java Introduce Annotations?

Before annotations, Java developers used XML files to configure beans, dependencies, and logic.

Problems with XML:

  1. Complex to read and write

  2. Error-prone (misplaced tags, typos)

  3. Requires manual synchronization with code

  4. Difficult to debug and maintain


How Annotations Solve It

Annotations embed the configuration within the code, making it:

  • Easier to understand

  • Less error-prone

  • Faster to develop

Comparison Example:

XML-based configuration:

<bean id="userService" class="com.app.UserService"/>

Annotation-based configuration:

@Service
public class UserService { }

XML vs Annotation-Based Java Platforms

Feature/ComponentXML-BasedAnnotation-Based
JDK≤ 1.41.5 and above
JDBC2.04.0+
Servlets2.53.0+
Struts1.x2.x
JSF1.x1.2+
Spring≤ 2.42.5 and above
EJB2.x3.x

Note: XML is still useful in centralized config files, but annotations dominate most modern Java codebases.


Final Thoughts

Mastering these Java basics — from naming rules to program layout and modern annotation use — makes you a cleaner, more professional Java developer. Follow these practices not just to impress interviewers or peers, but to make your own code easy to maintain and scale.


What’s Next?

In upcoming posts, I’ll explore:

  • Core Java concepts

  • Object-oriented programming in depth

Stay connected. Let’s grow together — one clean line of Java at a time.


1
Subscribe to my newsletter

Read articles from Lilavati Mhaske directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lilavati Mhaske
Lilavati Mhaske

Hi, I’m Lilavati Mhaske — a tech enthusiast, Java learner, and aspiring full stack developer. I created this blog to document and share my journey through Full Stack Java development, from the fundamentals to frameworks like Spring Boot, REST APIs, databases, and frontend integration. I believe in learning by doing—and explaining! Whether you're just starting out or brushing up your Java skills, I hope my posts make your learning smoother and more enjoyable. Let’s grow together, one line of code at a time. ☕💻