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 7519827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:58:01+00:00 2026-05-30T01:58:01+00:00

In below class, Store class has exactly 1 fruit as a field variable. I

  • 0

In below class, Store class has exactly 1 fruit as a field variable.

I want Store class to do the following two things. One is returning the fruit’s data only with read access, and the other is returning the fruit’s data with the write access. The data returned has type ByteBuffer.

For example, if someone get the ByteBuffer through getRead, I don’t want ByteBuffer to be modified at all. However, if someone access ByteBuffer through getWrite, then I am allowing him to modify the contents of memory pointed by ByteBuffer.

class Fruit {
    private ByteBuffer data;
    public ByteBuffer getData(){
        return data;
    }
}
class Store {
    Fruit p;
    public ByteBuffer getRead(){
        return p.getData();
    }

    public ByteBuffer getWrite(){
        return p.getData();
    }
}

Is there anyway that I can control this access privilege in Java when I am using ByteBuffer? Or, should I have 2 variables in Fruit class that has the same value but does the different thing?

  • 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-30T01:58:01+00:00Added an answer on May 30, 2026 at 1:58 am

    In this particular case it’s really easy using asReadOnlyBuffer:

    public ByteBuffer getRead(){
        return p.getData().asReadOnlyBuffer();
    }
    

    In general, there has to be some sort of wrapping object (as there is here) – Java doesn’t have the concept of a read-only “view” on an object from a language point of view; it has to be provided by code which prevents any mutations.

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

Sidebar

Related Questions

I have a data structure which is as given below: class File { public
I have a question, here are two classes below: class Base{ public: virtual void
I want build a data structure to store limited undo buffer, take store 6
Write a class ListNode which has the following properties: int value; ListNode *next; Provide
I want to be able use python descriptors in a class which has the
Say want to store the following: typedef std::function<void(int)> MyFunctionDecl; ..in a collection: typedef std::vector<MyFunctionDecl>
Map is a container class that is used to store the aggregate data... Its
I have a class that has a number of methods that follow the below
I have one problem Looking below: I have this model: class Shoes(models.Model): shop =
i need Enumarable value from below class but give error public static Enumerable LoadDataByName(string

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.