Javatpoint Logo
Javatpoint Logo

Contextual Keywords in Java

Contextual keywords formerly known as restricted identifiers and restricted keywords. contextual keywords are identified based on where they will appear in the syntactic grammar.

These are the keywords that provides the specific meaning in the code. These are not reserved keywords like abstract, new, final, try, import, etc.

Character sequences, formed from ASCII characters, are reserved for use as keywords that cannot be used as identifiers. Another 12-character sequences, also formed from ASCII characters, may be interpreted as keywords, depending on the context in which they appear.

So, reserved keywords and contextual keywords are two different things.

Reserved Keyword

There are the following reserved keywords in Java:

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
_ (underscore)

Contextual Keyword

Following are the contextual keywords in Java:

  • exports opens to var
  • module provides transitive with
  • open requires uses yield

A character sequence matching a contextual keyword is not treated as a keyword if any part of the sequence can be combined with the immediately preceding or following characters to form a different token.

So, the character sequence openmodule is interpreted as a single identifier rather than two contextual keywords, even at the start of a ModuleDeclaration. If two keywords are intended, they must be separated by whitespace or a comment.

We are being intentionally vague about how an implementation might disambiguate. The above note should be enough to make clear that this is an implementation choice, not something we're going to specify explicitly. Still, as the language evolves, designers will need to be careful about the implementation impact of new contextual keywords in certain contexts.

A variety of character sequences are sometimes assumed, incorrectly, to be keywords:

  • true and false are not keywords, but rather boolean literals.
  • nullis not a keyword, but rather the null literal.

The meaning of a name depends on the context in which it is used. The determination of the meaning of a name requires three steps:

  • First, context causes a name syntactically to fall into one of seven categories: ModuleName, PackageName, TypeName, ExpressionName, MethodName, PackageOrTypeName, or AmbiguousName.
    TypeName and MethodName are less expressive than the other five categories, because they are denoted with TypeIdentifier and UnqualifiedMethodIdentifier, respectively.
  • Second, a name that is initially classified by its context as an AmbiguousName or as a PackageOrTypeName is then reclassified to be a PackageName, TypeName, or ExpressionName.
  • Third, the resulting category then dictates the final determination of the meaning of the name (or a compile-time error if the name has no meaning).

The use of context helps to minimize name conflicts between entities of different kinds. Such conflicts will be rare if the naming conventions described in 6.1 are followed. Nevertheless, conflicts may arise unintentionally as types developed by different programmers or different organizations evolve. For example, types, methods, and fields may have the same name. It is always possible to distinguish between a method and a field with the same name, since the context of a use always tells whether a method is intended.

Identifiers that act as keywords only in specific language structures. The difference with keywords is that context keywords can be used as identifiers in other parts of the code. Certain context keywords (such as partial and where) have special meanings in two or more contexts.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA