Sunday, August 1, 2010

Access Modifiers:

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

File myProject/source/pkgA/

File myProject/source/pkgB/


Go to directory myProject and create classes directory

While you are still in myProject use the following command to compile both and

/myProject$ javac -d classes source/pkgA/ source/pkgB/

cd into classes directory and use the following command to run

/myProject/classes$ java pkgB.Baz

The output is 7

Now go back and re-write

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

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

No comments:

Post a Comment