Path of Package and Class

This question is about how to correctly use static variable of a class in another package.

1. Given the two source files:

1.  package com.sun;
2.  public class PkgAccess {
3.    public static int tiger = 1414;
4.  }

And:

1.  import static com.sun.PkgAccess.*;
2.
3.  public class PkgAccess2 {
4.
5.    int x1 = PkgAccess.tiger;
6.    int x2 = tiger;
7.    int x3 = com.sun.PkgAccess.tiger;
8.    int x4 = sun.PkgAccess.tiger;
9.  }

Which two are true? (Choose two.)

  • 1 - The PkgAccess2 class compiles.
  • 2 - Compilation fails due to an error on line 5.
  • 3 - Compilation fails due to an error on line 6.
  • 4 - Compilation fails due to an error on line 7.
  • 5 - Compilation fails due to an error on line 8.
  • 6 - The PkgAccess and PkgAccess2 classes both compile.

Answer: 2 and 5 are true.

Explanation : When you say "import static com.sun.PkgAccess.*;" you are importing every static value of the class PkgAccess, so you can call that value using

com.sun.PkgAccess.tiger  // full path to access static "tiger" variable
tiger // use directly the static variable (because we import it !)

you CAN'T SAY

sun.PkgAccess.tiger // because the path is incorrect
PkgAccess.tiger // same as above

When you input those code in IDE like Netbeans, it will let you change it.

Category >> Classes & Interfaces  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>