Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:38:57+00:00 2026-06-01T17:38:57+00:00

Since i can access private variables of an inner class directly and … the

  • 0

Since i can access private variables of an inner class directly and … the only way to access inner class is via the outer class .. is there any value in having public accessor/ mutator methods for an inner class?

public class Outer {
    Inner   i;

    Outer() {
        i.v = 5;
    }

    private class Inner {
        private int v;
    }  
}

For the purposes of this question, assume that no lazy instantiation or other data manipulation is needed upon access.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:38:58+00:00Added an answer on June 1, 2026 at 5:38 pm

    Well, making the fields non-private would avoid synthetic methods being created to access those private members. In your example, the compiled Outer$Inner class contains a synthetic method like this:

    static int access$002(Outer$Inner, int);
       Code:
          0: aload_0
          1: iload_1
          2: dup_x1
          3: putfield      #1                  // Field v:I
          6: ireturn
    

    Maybe that will be inlined down to nothing, maybe it won’t… but unless there’s any reason to force its existence, why not just give v the default access?

    As for using accessor methods rather than using the fields directly… that’s more of a matter of taste. You can’t prevent the outer class having direct access to the fields, so it’s a little like asking whether you should create accessors for private fields within a class.

    As mentioned in another answer, using accessors makes it more refactoring-friendly. Is the nested class just an implementation detail which will never need to move? If so, maybe that’s fine. I find that if I’ve got a class like that which is just encapsulating fields with no other behaviour, I make the fields public (within a private class, don’t forget) to advertise “Hey, I’m not checking anything here…”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since we can access the private data member of base class in the derived
Since you can call the the Soundcloud API via XHR (because of the CORS
Since Postgres can only add columns at the end of tables, I end up
In C++, since you can have class-instances as members in structs, the compiler has
I know it can't be done since using var can only be done for
Since I migrated to Snow Leopard I can't run any Rake command from the
A protected class member in Java by convention, can be accessed only from within
Possible Duplicate: Cannot access private member in singleton class destructor I'm implementing a singleton
I have a question about righteous way of programming in Python... Maybe there can
Since I can't put up a screenshot of my storyboard, I'll try to explain:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.