Why we should follow the naming conventions in Java?


Naming conventions in Java make programs more understandable by making them easier to read.

In Java, class names generally should be nouns, in title case with the first letter of each separate word capitalized. and interface names generally should be adjectives, in title case with the first letter of each separate word capitalized.

Why should follow the Java naming standards

  • To reduce the effort needed to read and understand source code.
  • To enable code reviews to focus on more important issues than arguing over syntax and naming standards.
  • To enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.

Naming Conventions for a different type of Identifiers

Package

  • The Package name should be in all lower case.

Example

package com.tutorialspoint;

Interface

  • The Interface name should start with an upper case character.

Example

interface TutorialsPointInterface {
  // some statements
}

Class

  • All the words of a class name should start with an upper case character.

Example

class TutorialsPointClass {
  // some statements
}

Method

  • Methods should be verbs with the first letter lowercase and the first letter of each internal word capitalized.

Example

class TutorialsPointClass {
   void printMessage() {
   }
}

Variable

  • The first word should be in lower case and the internal words start with capital letters.
  • Variable names should not start with underscore _ or dollar sign $ characters.

Example

class TutorialsPointClass {
   int rollNum;
   String firstName;
   String lastName;
}

Constant

  • All the characters should be in upper case.

Example

class TutorialsPointClass {
   public static final int MAX_score = 100;
}

Updated on: 06-Feb-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements