Naming Conventions in Java

Posted on

Java Naming Convention :

A naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types, functions, and other entities in source code and documentation.

Why Naming Conventions are used ?

  • 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. 

The main naming conventions in java are

  • Class and Interface names : should start with uppercase letter and be a noun g. String, Color, Button, System, Thread etc. In mixed case with the first letter of each internal word capitalized e.g. ImageBuilder, SetConnection etc. Try to keep your class and interface names simple and descriptive. Acronymes and abbreviations are should not be used in class names.
  • Method names : method names should be verbs and should start with lowercase letters. In mixed case with the first letter of each internal word should be capitalized. g. getData( ), SetData( ) etc.
  • Variable names : Use meaningful names for variables. Variable name must define the exact explanation of its content.Use short enough and long enough variable names in each scope of code. Generally length may be 1 char for loop counters, 1 word for condition/loop variables, 1-2 words for methods, 2-3 words for classes, 3-4 words for globals. Variable names should not be too long. E.g. Rollno, name
  • Constants :The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores (“_”). E.g. MAX_VALUE, MIN_VALUE
  • Package names : Package names should be unique and consist of lowercase letters. Underscores may be used if necessary.e.g. fish.fish_finder.