What is the difference between qualified name and a field access expression?

一世执手 提交于 2019-12-03 14:22:08

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.

The meaning of a field access expression is determined using the same rules as for qualified names, but limited by the fact that an expression cannot denote a package, class type, or interface type.

found the above text in oracle's web site.

so in simple terms:

  1. Qualified name means which carries parent info in declaration. for e.g. Pack1.Pack2.Pack3.Class1 & Pack1.Pack2.Pack4.Class2

in Pack4, we can access Class1 in either of following ways Pack3.Class1 or Pack2.Pack3.Class1 or Pack1.Pack2.Pack3.Class1, where the last one would be fully Qualified name.

  1. field access expression is a subtype of qualified name, but as name says, it is for accessing field

So Qualified names may refer to Packge, Class, Interface but not fields, while field access expression would refer only to fields

REF: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.11 http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.5.6.2

qualified name make sense in terms of class name (think of it in terms of static). where as field access expression is like specifying the Fully Qualified Name in terms of reference of for a particular object of a class including method names.

Example :

public class A
{
    public static void method1()
    {//does something
    }
}

public class B 
{
     public int dummy;
     public void hello()
     {
          System.out.println("Hello!");
     }
}

public class Main 
{
     public static void main(String[] args) 
     {
          B b = new B();
          b.dummy=1;
          b.hello();
     }
}

here in the above classes if we say

A.method1() 

it is more of a qualified name, where as

b.hello(); 
b.dummy

is more of a field access expression.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!