xhassassin
xhassassin

Reputation: 283

Java scope modifiers in private nested class

Suppose I have some java code as follows:

public class MainClass {
  private static class NestedClass {
    <modifier> int field;
  }
}

Does the modifier do anything? IE is there any difference between saying public int field vs private int field since in both cases the field is only accessible in MainClass? What's the standard style guideline for code like this? I understand that if NestedClass wasn't private then the modifier might differentiate scope but since it is private the valid access patterns for field will be the same no matter what the modifier is, correct?

Upvotes: 0

Views: 43

Answers (2)

Barinder Grewal
Barinder Grewal

Reputation: 59

The MainClass can access all the members of the NestedClass regardless of their modifier.

Access Modifier in NestedClass is used only to restrict any external class to access the variables or methods of the NestedClass.

Upvotes: 0

user4571931
user4571931

Reputation:

Outer class, can access it's members (inner class). You can use the access specifiers for the inner class (nested class) if you wants to give restrictions to some external class which are accessing it

Upvotes: 1

Related Questions