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

The Archive Base Latest Questions

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

I would like to share data between the objects. Actually, for example, there is

  • 0

I would like to share data between the objects.
Actually, for example, there is a World composed of Water and Individuals.
I would like the Individuals to access the Water.
I looked at transforming the Individual table in Collection, or making the Water a Singleton, but really, what would be the best design?
(we might have multi threading processes on the Individual object… So we might need a Thread safe implementation.)
(Also, I wonder about passing by argument or instantiate the Water for each Individuals, but it seems not to be the most efficient implementation (processing instantiation) ?…)

A simple set of objects in order to fix the ideas (this set does not share the Water at all, the Individuals display ‘null’ here) :

package myworld.testing;

public class World {

    static Water evolution;
    static Individual [] tribe;

    public World(){
        evolution=new Water();
    }
    public static Water getWater(){
        return evolution;
    }

    public static void main(String[] args) {
        evolution=new Water();
        System.out.println("info "+evolution.get());
        tribe = new Individual[10];
        for(Individual individual : tribe){
            System.out.println("trace "+individual);
        }

        evolution=new Water("sprakly");
        System.out.println("info "+evolution.get());
        for(Individual individual : tribe){
            System.out.println("trace "+individual);
        }

        evolution.set("salty");
        System.out.println("info "+evolution.get());
        for(Individual individual : tribe){
            System.out.println("trace "+individual);
        }
    }

}

package myworld.testing;

public class Individual {
    int data;
    public Individual(){
        data=0;
    }
    public Individual(int i){
        data=i;
    }
    public void set(int i){
        data=i;
    }
    public int get(){
        return data;
    }
    public String toString(){
        return World.getWater().get()+" "+data;
    }
}

package myworld.testing;

public class Water {
    private static Water instance = null;
    String type;
    public static Water getInstance(){
        if(instance == null) {
             instance = new Water();
          }
          return instance;
    }
    public Water(){
        type="1";
    }
    public Water(String str){
        type=str;
    }
    public void set(String str){
        type=str;
    }
    public String get(){
        return type;
    }
}

Thank you so much for your help and advice in advance.
Feel free to share your thoughts 🙂

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

    The reason your getting null values while iterating over the array of Individuals is that, for each array element, you haven’t created an instance whose reference will occupy it. Instantiating an array merely creates a sequential memory block of null pointers. The correct solution is:

    tribe = new Individual[10];
    for(int i = 0; i < tribe.length; i++) {
        tribe[i] = new Individual();
    }
    

    In regards to your question about “individuals accessing the water,” you must declare the encapsulated instance of Water in World with the public visibility modifier to grant access to all classes outside the package containing World.

    Given that Individual and World reside in the same package, using the public modifier isn’t necessary.

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

Sidebar

Related Questions

Helllo, I would like to share small amounts of data (< 1K) between python
I would like to share 2 folders of content ('stylesheets' and 'graphics') between multiple
Problem: I would like to share code between multiple assemblies. This shared code will
I would like to know if there is some way to share a variable
I would like to exchange objects between two python interpreters working on the same
I would like to share my Oracle SQL Developer configuration across my several computers
My problem is: I have 3 procs that would like to share config loaded
I have 10 websites on an IP. I would like to share the IP
I would like to display static (shared) objects at runtime in a PropertyGrid but
We have published a json api to share data between a few domains. We

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.