Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple name and qualified name

Tags:

java

I'm reading JLS 8 and in Chapter 6 is written:

A qualified name N.x may be used to refer to a member of a package or reference type, where N is a simple or qualified name and x is an identifier. If N names a package, then x is a member of that package, which is either a class or interface type or a subpackage. If N names a reference type or a variable of a reference type, then x names a member of that type, which is either a class, an interface, a field, or a method.

so I could imagine that having this:

class C
{
    public int n;
}

int j;
C c = new C(); 
j = 11;  
c.n = 11;

j is a simple name while c.n is a qualified name.

However in 6.2 things get complicated. Is given this code:

class Test { 
 public static void main(String[] args) { 
 Class c = System.out.getClass(); 
 System.out.println(c.toString().length() + 
 args[0].length() + args.length); 
 } 
}

and after is said:

The occurrence of length in args.length is a name because args.length is a qualified name (§6.5.6.2) and not a field access expression (§15.11). A field access expression, as well as a method invocation expression, a method reference expression, and a qualified class instance creation expression, uses an identifier rather than a name to denote the member of interest. Thus, the occurrence of length in args[0].length() is not a name, but rather an identifier appearing in a method invocation expression.

so I think I understand that not all expressions are qualified names and even my expression c.n

Honestly I can not understand the distinction can anyone help me?

like image 485
xdevel2000 Avatar asked Nov 25 '16 13:11

xdevel2000


1 Answers

In your exemple:

int j; is a simple expression name because it consists of a single Identifier

For qualified example, from the JLS 6.5.6.2. Qualified Expression Names :

If an expression name is of the form Q.Id, then Q has already been classified as a package name, a type name, or an expression name.

In c.n, c is an expression name, n is a field of class T (class C in your exemple). So c.n is a qualified expression name.

args.length is also a qualified expression name. args is an array so it's not really a specific class (no .class file but run-time type signature is generated though) but it's still an object with a field named length.

args[0].length() is not a qualified name because length() is not a member of the class. It's an identifier of a method invocation expression.

MethodInvocation:
  MethodName ( [ArgumentList] )
  TypeName . [TypeArguments] Identifier ( [ArgumentList] )
  ExpressionName . [TypeArguments] Identifier ( [ArgumentList] )
  Primary . [TypeArguments] Identifier ( [ArgumentList] )
  super . [TypeArguments] Identifier ( [ArgumentList] )
  TypeName . super . [TypeArguments] Identifier ( [ArgumentList] )

ArgumentList:
  Expression {, Expression}

Cf. https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12

like image 170
alain.janinm Avatar answered Oct 04 '22 23:10

alain.janinm