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

  • Home
  • SEARCH
  • 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 834209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:38:23+00:00 2026-05-15T04:38:23+00:00

Possible Duplicates: ‘Friends’ equivalent for Java? Is there a way to simulate the C++

  • 0

Possible Duplicates:
‘Friends’ equivalent for Java?
Is there a way to simulate the C++ ‘friend’ concept in Java?

In C++ there is a concept of a “friend”, which has access to a class’s private variables and functions. So if you have:

class Foo {
    friend class Bar;
private:
    int x;
}

then any instance of the class Bar can modify any Foo instance’s x member, despite it being private, because Bar is a friend of Foo.

Now I have a situation in Java where this functionality would come in handy, but of course it doesn’t exist in Java.

There are three classes: Database, Modifier, Viewer. The Database is just a collection of variables (like a struct). Modifier should be “friends” with Database; that is, it should be able to read and write its variables directly. But Viewer should only be able to read Database’s variables.

How is this best implemented? Is there a good way to enforce Viewer’s read-only access of Database?

  • 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-05-15T04:38:23+00:00Added an answer on May 15, 2026 at 4:38 am

    There are varying opinions on how pure you should be, but to me you shouldn’t give anyone access to any fields. Use getters and setters.

    If you need to segregate who can read and who can write, you can throw interfaces into the mix. Define an interface with just the getters and you can restrict your viewer to read only.

    Robert Harvey added a comment that points to other options, like using a different access modifier for class or package level access.

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

Sidebar

Related Questions

Possible Duplicates: Why ‘this’ is a pointer and not a reference? SAFE Pointer to
Possible Duplicates: What does ‘: number’ after a struct field mean? What does ‘unsigned
Possible Duplicates: What does the ‘k’ prefix indicate in Apple’s APIs? Lower case “k”
Possible Duplicate: ‘CONTINUE’ keyword in Oracle 10g PL/SQL I am Using Oracle 9i and
Possible Duplicate: Python ‘==’ vs ‘is’ comparing strings, ‘is’ fails sometimes, why? Is a
Possible Duplicate: C# ‘var’ vs specific type performance I use the 'var' keyword extensively.
Possible Duplicates: Why should we typedef a struct so often in C? Difference between
Possible Duplicates: is “else if” faster than “switch() case” ? What is the relative
Possible Duplicates: Best “official” scripting language for Windows programmers Universal scripting language for Windows

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.