Have a question?
Message sent Close
View Categories

Variable Naming convention

Table of Contents

Variable Naming convention 

📄
filename.js
int age;           // Good
int a;             // Bad
double salary;     // Good
double sal;        // Bad

  1. Camel Case Notation: Use camel case for variable names, where the first word is in lowercase and each subsequent word starts with an uppercase letter.
📄
filename.js
int employeeCount;   // Good
int employeecount;   // Bad
int employee_count;  // Bad

2. Avoid Reserved Words: Do not use Java reserved keywords as variable names.

📄
filename.js
int class;    // Bad
int public;   // Bad

3. Start with a Letter: Variable names should start with a letter (a-z, A-Z) or an underscore (_), and not with a digit.

📄
filename.js
int age;       // Good
int _age;      // Not advised to use 
int 1age;      // Bad

4. Case Sensitivity: Remember that Java is case-sensitive, so age and Age would be considered different variables.

📄
filename.js
int age;
int Age;      // Different variable from `age`

5. Constant Variables: Use all uppercase letters with words separated by underscores. Constants are typically declared using the final keyword.

📄
filename.js
static final int MAX_USERS = 100;
static final double PI = 3.14159;

📄
filename.js
int numberOfStudents;
double averageTemperature;
String customerName;
final int MAX_RETRIES = 5;

📄
filename.js
int num;              // Not descriptive
double avgTemp;       // Use full words for clarity
String CustName;      // Avoid capitalizing the first letter
final int MAXRETRIES; // Separate words with underscores for constants

Summary #

  • Descriptive Names: Choose names that clearly describe the variable’s purpose.
  • Camel Case Notation: Use camel case for variable names.
  • Avoid Keywords: Do not use Java reserved keywords as variable names.
  • Start with a Letter: Begin variable names with a letter or underscore.
  • Case Sensitivity: Be aware that Java is case-sensitive.
  • Constants: Use all uppercase letters with underscores to separate words for constants.