Sunday, November 24, 2013

Question - Access Specifiers

Given:
 package test;
 

class Target {
    public String name = "hello";
 }

What can directly access and change the value of the variable name?
A. any class
B. only the Target class
C. any class in the test package
D. any class that extends Target

Execution:
Class is not public but default.
Variable is public
So class is accessible within package which makes it's answer C, whether it extends or not.

Result:
C. any class in the test package

No comments:

Post a Comment