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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:27:50+00:00 2026-05-14T05:27:50+00:00

I’m using Java. I want to have a setter method of one class that

  • 0

I’m using Java. I want to have a setter method of one class that is only accessible to an “owning” object of another class. I’ll be more specific:

I have Node and Port interfaces. Every node has a set of Ports, and each Port belongs to a single Node. A Port may be enabled or disabled, handled with a simple boolean property. However, I only want to allow the Node that “owns” the Port to set its enabled/disabled property. This should happen when Node.fixYourPorts() is called (OK, it has a different name, but I’m trying to simplify).

So, I want everybody to be able to call port.isEnabled(), but only the owning Node should be able to call port.setEnabled().

I can think of a few ways to do this, but I’m not sure which is best, and I’m concerned that I may be thinking about this problem the wrong way. Ideas so far:

  1. Each Node implementation would need a matching Port implementation in the same package, and PortImpl.setEnabled() would be package-access. Doesn’t guarantee that one Node won’t change another Node’s Ports, but that’s close enough for me. The Node code is much more under my control than any random client code that may get a Port instance.

  2. Instead of programming to the interface Port, program to an abstract base class, AbstractPort which is a static nested class inside the interface Node. This base class would provide the setter method, so then Node implementations could call it. Not sure if this would even work.

  3. Write another layer of code on top of all of this that the client would not be able to poke through. Add setEnabled to the Port interface. The layer of code on top would make sure the setter is not exposed.

  4. Add the setter to the interface. If you as the client enable or disable a port yourself, then you need to be prepared for things to not work correctly. Add a comment to this effect.

This is all complicated by the fact that Node and Port implementations will be persisted to a DB using Hibernate.

Any help?

  • 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-14T05:27:51+00:00Added an answer on May 14, 2026 at 5:27 am

    Can you expose the Port to client classes, and expose it to the Node object with an additional interface providing the setter method ? e.g. the component constructing it will create a (say) ConfigurablePort (extending Port) and pass this to the Node, but pass the Port back to the client as a mere Port.

    interface ConfigurablePort extends Port {
       void setValue();
    }
    
    // in some factory... Or perhaps in the Node itself?
    public Port createPortForNode(Node node) {
       ConfigurablePort port = new Port();
       node.setPort(port);  // as Configurable
       return port; // as immutable
    }
    

    So you’re not worried here about packaging issues, and you avoid the headache of having to implement methods (even to throw an exception) that you don’t want to.

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

Sidebar

Ask A Question

Stats

  • Questions 414k
  • Answers 415k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I just created a project. It has an ASP.NET HttpHandler… May 15, 2026 at 8:51 am
  • Editorial Team
    Editorial Team added an answer You can but not in that way. this is referred… May 15, 2026 at 8:51 am
  • Editorial Team
    Editorial Team added an answer You're looking for an UpDown control, also called a spinbox.… May 15, 2026 at 8:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.