In Java, which access level applies to a class member when no access modifier…
2024
In Java, which access level applies to a class member when no access modifier is written, so that the member is accessible only from code in the same package?
Answer: C. package-private — ConceptJava defines four member-access levels. Three are written with the modifiers public, protected, and private; package-private access is selected by…
- A.
private
- B.
public
- C.
package-private
- D.
protected
- E.
static
Attempted by 439 students.
Show answer & explanation
Correct answer: C
Concept
Java defines four member-access levels. Three are written with the modifiers public, protected, and private; package-private access is selected by writing no access modifier.
An access level sets the boundary from which a member may be referenced. Package-private access uses the package as that boundary.
Application
The stem explicitly describes a member declared without an access modifier and visible to code in the same package. That declaration therefore has package-private access.
Contrast
private restricts member access to the declaring top-level class and its nested declarations.
public permits member access wherever the enclosing type is accessible.
protected covers the same package and qualifying subclasses outside that package.
static controls whether a member belongs to the class rather than to individual objects; it is not an access level.
Cross-check
Oracle’s Java access-control table marks “no modifier” as accessible in the class and package, but not from unrelated subclasses outside the package or the world at large. This is the package-private pattern.
Result
The applicable access level is package-private, expressed by omitting an access modifier.