|
|
|
package myLib;
public class ClassA
{
/* ----------------------------------------
Default (no access specfier) access
---------------------------------------- */
static void Method81()
{
System.out.println(
"*** This is (no specifier) Method81() of ClassA in PACKAGE myLib");
}
}
|
|
|
We will set up the following scenario:
|
The method main() in myProg1.java is not inside package myLib:
/* NOT a class inside package myLib */
import myLib.*;
public class myProg1
{
public static void main(String[] args)
{
ClassA.Method81(); // Try to invoke the PACKAGE access method (fail)
}
}
|
Result: compile error
>>> javac myProg1.java
myProg1.java:9: Method81() is not public in myLib.ClassA; cannot be accessed from outside package
ClassA.Method81(); // Try to invoke the PACKAGE access method (fail)
^
1 error
|
How to run the program:
|
Note:
|
|
We will set up the following scenario:
|
The method helper() inside ClassB is inside package myLib and has public access
package myLib;
public class ClassB
{
/* ------------------------------
PUBLIC access
------------------------------ */
public static void helper()
{
System.out.println(
"*** This is PUBLIC helper() of ClassB in PACKAGE myLib");
System.out.println(
"--- I will invoke the PACKAGE access Method81() in ClassA");
ClassA.Method81(); // invoke (default access) method !
}
}
|
The method main() will invoke ClassB.helper() (which in turn will invoke ClassA.Method81()):
import myLib.*;
public class myProg2
{
public static void main(String[] args)
{
ClassB.helper();
}
}
|
Result:
*** This is PUBLIC helper() of ClassB in PACKAGE myLib --- I will invoke the PACKAGE access Method81() in ClassA *** This is (no specifier) Method81() of ClassA in PACKAGE myLib |
This experiment shows that the method helper() (that is inside the same package) can invoke the method ClassA.Method81() !!
How to run the program:
|