Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reserved words as names or identifiers

People also ask

Can a reserved word be used as an identifier?

Reserved words can never be used as identifiers. Keywords can be used as identifiers, but this is not recommended. Some of these words are also reserved by SQL.

What are reserved words called?

Reserved words (also called keywords) are defined with predefined meaning and syntax in the language. These keywords have to be used to develop programming instructions. Reserved words can't be used as identifiers for other programming elements like name of variable, function etc.

What is the difference between a reserved word and identifier?

A reserved word is a keyword in a programming language that serves to mark the structure of a state-ment. For example, the keywords if and else are reserved words. A standard identifier is a key-word that defines a type.

What do you mean by reserved word?

a word in a programming language or computer system that has a fixed meaning and therefore cannot be redefined by a programmer.


This is a valid question. Such a thing is possible in other languages. In C#, prefix the identifier with @ (as asked before); in Delphi, prefix with &. But Java offers no such feature (partly because it doesn't really need to interact with identifiers defined by other languages the way the .Net world does).


No, there is no way. That's why they're labeled "reserved".


Most often this issue comes up for "class", in this case it is customary to write "clazz".


No, you can't do this. For more information please go to JLS Sections 3.8, 3.9

The following character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers (§3.8):

Keyword: one of
        abstract    continue    for           new          switch
        assert      default     if            package      synchronized
        boolean     do          goto          private      this
        break       double      implements    protected    throw
        byte        else        import        public       throws
        case        enum        instanceof    return       transient
        catch       extends     int           short        try
        char        final       interface     static       void 
        class       finally     long          strictfp     volatile
        const       float       native        super        while

Strictly speaking you can't, unless you get your hands on a buggy compiler implementation that doesn't adhere to the Java language spec.

But where there's a will, there's a way. Copy the following code into your IDE, switch the source file encoding to UTF-16 and here we go:

public class HelloWorld {

    public static void main(String[] args) {

        HelloWorld.nеw();
    }

    public static void nеw () {
        System.out.println("Hello,World");
    }

}

This code is a well-formed and valid Java class. However, as you have guessed there is a little trick: the 'е' character within "new" identifier does not belong to the ASCII character set, it is actually a cyrrilic 'е' (prounanced 'YE').

Current Java language spec explicitly permits, and this an important point to make, the use of Unicode for naming identifiers. That means that one has an ability to freely call her or his classes in French, Chinise or Russian if they wish. It is also possible to mix and match the alphabets within code. And historically, some letters within Latin and other alphabets are lookalikes.

As a result: no, you can't use the reserved words as identifiers, but you can use identifiers that look exactly like reserved words.

Whether anyone should be doing it is a totally different matter.