print
Advertisment
Advertisment

identifiers in java

In Java, an identifier is a sequence of characters used to give a name to a variable, method, class, or any other programming element. Identifiers are used to refer to the specific element within a program's code.

Java has several rules for constructing identifiers:

  • The first character must be a letter or an underscore character (_).
  • The rest of the characters may be letters, underscores, or digits.
  • Identifiers are case-sensitive, meaning that uppercase and lowercase letters are considered different characters.
  • Identifiers cannot be a Java keyword or reserved word, such as "if," "else," "while," "class," or "public."
  • Instance variables are always visible in all methods of the class. They always refer to the invocation object.
  • The public instance variables are visible outside the class, and they can be accessed. through a reference to the object to which they belong.

Examples of valid identifiers in Java

  • myVariable
  • myMethod
  • myClass
  • my_package_name

Examples of invalid identifiers

  • 1stVariable (because it starts with a digit)
  • public (because it is a reserved keyword)
  • my-variable (because hyphens are not allowed in Java identifiers)

It is good programming practice to choose meaningful and descriptive identifiers that reflect the purpose of the element they are associated with. This makes it easier to understand the code and maintain it over time.

Example

MyClass.java

public class MyClass {
    // Example of instance variable
    private int myVariable;

    // Example of constructor
    public MyClass(int myVariable) {
        this.myVariable = myVariable;
    }

    // Example of method with parameter
    public void myMethod(String parameter) {
        System.out.println("Parameter value is: " + parameter);
    }

    // Example of constant variable
    public static final int MY_CONSTANT = 100;

    // Example of main method
    public static void main(String[] args) {
        MyClass myObject = new MyClass(42);
        myObject.myMethod("Hello world!");
        System.out.println("My constant value is: " + MY_CONSTANT);
    }
}

Output :


Parameter value is: Hello world!
My constant value is: 100
Advertisment

In this example, we have several identifiers:

  • MyClass: the name of the class.
  • myVariable: the name of an instance variable.
  • MyClass: the name of a constructor.
  • myMethod: the name of a method.
  • parameter: the name of a parameter in the myMethod method.
  • MY_CONSTANT: the name of a constant variable.
  • main: the name of the main method.

All of these identifiers follow the rules for constructing identifiers in Java. Note that MyClass and myVariable are both lowercase letters, while MY_CONSTANT is in all uppercase letters. This is a common convention for constant variables in Java.

Correct Identifiers & incorrect Identifiers in java with Explanation

Correct Identifiers

MyClass.java

// Examples of correct identifiers:
int myVariable;
String myString;
MyClass myClass;
myMethod();
_myVariable;
$myVariable;

Explanation:

  • myVariable: starts with a lowercase letter, followed by more lowercase letters.
  • myString: starts with a lowercase letter, followed by uppercase and lowercase letters.
  • myClass: starts with a lowercase letter, followed by uppercase and lowercase letters.
  • myMethod(): starts with a lowercase letter, followed by uppercase and lowercase letters, and ends with parentheses.
  • _myVariable: starts with an underscore character, followed by lowercase letters.
  • $myVariable: starts with a dollar sign character, followed by lowercase letters.

Incorrect Identifiers

MyClass.java

// Examples of incorrect identifiers:
int 1stVariable;
String My-String;
MyClass my class;
myMethod(int parameter);
public int myVariable;

Explanation:

  • 1stVariable: starts with a digit, which is not allowed in Java identifiers.
  • My-String: contains a hyphen, which is not allowed in Java identifiers.
  • my class: contains a space, which is not allowed in Java identifiers.
  • myMethod(int parameter): contains parentheses and a parameter declaration, which are not part of the method name.
  • public int myVariable: starts with a reserved keyword, "public", which is not allowed as an identifier in Java.
Advertisment

Why myMethod(int parameter) not a valid identifier?

myMethod(int parameter) is not a valid identifier in Java, because it contains parentheses and a parameter declaration, which are not part of the identifier name. In Java, a method identifier consists of the method name and its parameter types (if any), but it does not include the parameter names.

For example, if we have a method with the following signature:

Identifier.java

public void myMethod(int parameter) {
    // method body
}

The identifier for this method is simply "myMethod(int)", because it consists of the method name "myMethod" and the parameter type "int". Note that the parameter name "parameter" is not part of the method identifier.

It's important to follow Java's rules for constructing identifiers in order to avoid syntax errors and make code more readable and maintainable.

Advertisment
Advertisment
arrow_upward