Sunday, August 1, 2010

Access Modifiers: Baz.java

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 1, Question 5

File myProject/source/pkgA/Foo.java













File myProject/source/pkgB/Baz.java


 









Go to directory myProject and create classes directory

While you are still in myProject use the following command to compile both Foo.java and Baz.java:

/myProject$ javac -d classes source/pkgA/Foo.java source/pkgB/Baz.java

cd into classes directory and use the following command to run Baz.java

/myProject/classes$ java pkgB.Baz

The output is 7

Now go back and re-write Baz.java
 

















Go to myProject directory and compile. You will get two errors

source/pkgB/Baz.java:6: a is not public in pkgA.Foo; cannot be accessed from outside package
System.out.print(" " + f.a);
^
source/pkgB/Baz.java:7: b has protected access in pkgA.Foo
System.out.print(" " + f.b);
^
2 errors


No comments:

Post a Comment