Identifier | Naming Rules In Java

Identifier:

A name in java program is called identifier. It may be class name, method name, variable name and label name.

Example:
class Test {
public static void main(String args[]){
int a = 20;
}
}

Here Test, main, String[], args, a are identifiers.

Rules to define java identifiers:

Rule 1: The only allowed characters in java identifiers are:
1) a to z
2) A to Z
3) 0 to 9
4) _
5) $
Rule 2: If we are using any other character we will get compile time error.
Example:
1) total_number-------valid
2) Total#------------------invalid

Rule 3: identifiers are not allowed to starts with digit.
Example:
1) ABC123---------valid
2) 123ABC---------invalid
Rule 4: java identifiers are case sensitive up course java language itself treated as case sensitive language.
Rule 5: There is no length limit for java identifiers but it is not recommended to take more than 15 lengths.
Rule 6: We can’t use reserved words as identifiers.
Example: int if=10; --------------invalid
Rule 7: All predefined java class names and interface names we use as identifiers.
Example 1:
class Test
{
public static void main(String[] args){
int String=10;
System.out.println(String);
}}
Output:
10
Example 2:
class Test
{
public static void main(String[] args){
int Runnable=10;
System.out.println(Runnable);
}}
Output:
10

Please check the below table where each row represents a different type of identifier, along with its corresponding description and example.
Identifiers Description Example
Variable Identifier A name given to a variable to uniquely identify it within the program. int age;
Method Identifier A name given to a method (function) to uniquely identify it within the program. void printMessage() { ... }
Class Identifier A name given to a class to uniquely identify it within the program. public class MyClass { ... }
Package Identifier A name given to a package to uniquely identify it within the program. package com.example.myapp;
Interface Identifier A name given to an interface to uniquely identify it within the program. public interface MyInterface { ... }
Constant Identifier A name given to a constant value that cannot be changed during the execution of the program. final int MAX_VALUE = 100;
Label Identifier A name given to a label used for flow control statements like loops or branches. outerLoop: for (int i = 0; i < 5; i++) { ... }
Enum Identifier A name given to an enumerated type to define a set of named values. enum Color { RED, GREEN, BLUE }
Type Parameter Identifier A name given to a type parameter in generic classes or methods. class MyGenericClass<T> { ... }
Prev‹

Post a Comment

Previous Post Next Post