0

I am developing a Java Program, I have 2 different classes that need to share variables with each other. So for this purpose I created a third class, which contains these shared variables. Basically, first class has its own flag in this third class, and second class has its own flag in this third class. And I add "extend ThirdClass" to other classes' definition, of course.

My problem is when I change the value of first class' flag in the first class' main method, second class is not able to see this change. Namely, it sees the flag with inital value.

I think this is about static variables. But I do not know the solution.

Actually, I have two servers and multi clients. I am trying to simulate differenet situations of these servers for my distributed systems course. Each server has its own data table that is synchronized with other. By simulation, I mean server may be down or up, and when client try to reach down server I need to direct it to other server. So, between clients and servers I need to send information.

How can I share variables between two classes, and easily modify these variables by any class without loss of previous modifications ?

Thank you.

3
  • 5
    The whole approach is broken. You need to study about OOP and then reaproach the problem you are trying to solve. Commented Nov 27, 2010 at 22:19
  • Why do so many people write JAVA instead of Java? Commented Nov 27, 2010 at 22:29
  • 1
    is this homework? If it is, please tag it as so. Commented Nov 27, 2010 at 22:35

2 Answers 2

4

EDIT -- this answer is for a previous version of the question, that does not include anything about clients and multiple servers. This answer is about sharing data between instances of classes.

There are many ways to do something like this. If I understood your approach outlined in the first paragraph, you dont want class1 and class2 to extend class3. You want them to both have a reference to the same instance of class3. If they share a reference to the same object, then changing values on that object will be reflected in both classes. If they extend class3, like you said you tried, then both class1 and class2 will have the properties of class3, but they wont be sharing any data. If class 3 had a property

  Object value;

then instances of class1 and class2 would have separate references to separate instances of 'value'.

You need to undertand the relationship between a class, and object instance, and what extending a class means.

Note that this is not really a good way to share information between objects in a real program. You can run into all sorts of concurrency issues. The same is true for a solution that used a global mechanism implemented using static fields.

To do this with static fields do something like:

class SharedData {
    public static Object shared1;
}

and then in your class1 and class2 instances you can access

SharedData.shared1

for either set or get.

But I would not do anything like this in any sort of professional context.

Sign up to request clarification or add additional context in comments.

4 Comments

You answered the question alright, but I don't think it's the answer he needed. See the comment by @cherouvim
thank you for your explanation, but actually this is not what i need. Actually, I have two servers and multi clients. I am trying to simulate differenet situations of these servers for my distributed systems course. Each server has its own data table that is synchronized with other. By simulation, I mean server may be down or up, and when client try to reach down server I need to direct it to other server. So, between clients and servers I need to send information. This is not a professional context, but still i do not know what to do.
@jorn, yeah, based on his comment it clearly is not the right answer. but the way the question reads, thats what I thought he was trying to do;)
andevmj, you should update your question with the comment information.
0

You could solve this using statics if it makes sense. When a member is declared static for a class, everyone accessing that member refers to the same instance of that variable, but that doesn't really sound like what you're looking for.

If you want some data to be shared by two classes, you can put the data in a third class and when you create an instance of that third class, be sure the other two classes simply have access to it.

Could you outline what you're trying to accomplish?

1 Comment

Actually, I have two servers and multi clients. I am trying to simulate differenet situations of these servers for my distributed systems course. Each server has its own data table that is synchronized with other. By simulation, I mean server may be down or up, and when client try to reach down server I need to direct it to other server. So, between clients and servers I need to send information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.