• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to share member variables?  RSS feed

 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm a novice level programmer, by that I mean I can understand basic concepts such as OOP, Threads, Events and GUI, I've coded a little but I've always had this bothering me:
Explaining by example:



Okay, i'm pretty certain that code won't work, but I just want to show you conceptually, not actually care whether the code works or not. In case I wanted to get that code to work I should have probably used a Swing application to get a KeyListener in the first place, but I guess I know that, and if this was working, what would happen theoretically is, the code would run, initialize x to 10, and y to hello, then when a key is pressed, it will update x to 11 and y to hello world, I'm pretty sure that's what happens. (if not please correct me)

In this case, I used a inner class to update it's parent's members, I've seen this done before and I can vouch for the fact that it's a legitimate way to code a class.
(Please bear with me):

Now in this example:


So in this overly complicated example, i'm trying to share class X with both class Y and class Z, just that I have no idea how to share the members of class X with the different classes without making a new instance of X. In the previous example, I could access the parent's members because the inner class was implicitly capable of accessing the parent's members. However in this case, If "Y" starts an instance of X, then how do I access it? because it's a side by side class not a hierarchy for me to access the parent's members.

I hope you understand my question, simply put, I want to know how I can share one instance of a class, with more than one class, in my practical application, I want to have many small seemingly small classes that are shared together using the main class's main method.

Also(slightly off topic): just so you understand the question better, if I had used C in this case, I would have passed the address of the structure of X to Y and Z and then Y and Z would find out where the data is pointing to and then update the concerned variables, that's what I'm looking for in Java, I want a way to get access to the heap's single instance of X, and then update it's value.

Thanks for looking into this



 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand what you want to do, the posted code won't work. Every time you say new X(), you are getting a new instance of X. Since the fields in X are instance fields, they won't persist across instances. Maybe if you make X's fields static you'll get what you want. Try out some code and post it.
 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a feeling you have not understood what I meant to say, but that's probably my fault, I find it a little difficult to frame my question:

Let's say I have a non static object called X (X x = new X()) , I want to access X across All my source files, like I want to access X with Y.java and Z.java too, like if Y.java set x to 10, I want Z.java to set x to 20, and not just make a new instance of X every time, I want to share X as an object.

Hope that helps in understanding what I am saying.
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try static fields and see if they if the bill.
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lokesh Poovaragan wrote:Let's say I have a non static object called X (X x = new X()) , I want to access X across All my source files...


Let's deconstruct that one step at a time...

First of all there's no such thing as a "static object", nor is there any such thing as a "non-static object". The relevant concept is "static variable". A static variable may well contain a reference to an object, but that doesn't make the object static. It's just an object, any number of variables (static or otherwise) may refer to it.

Secondly, objects don't have names. Variables have names. Often programmers will be sloppy in the way they talk about variables and objects and talk as if the variable and the object it refers to are the same thing, but that's just for convenience. In this case you should avoid that short-cut and try to express your question in a way which preserves the distinction between variables and objects.

Thirdly, the name of the variable (not object) you're asking about is "x", not "X". The name of the variable's type is "X", but that isn't what you want to share with other classes. The problem here is that you've chosen bad class names and bad variable names. If you had meaningful names then it would be easier to talk about your question. I've found that when people use meaningless names in questions like this, they don't really have a use case they want to talk about. So it would be easier to answer your question if you had a real example of what you wanted to do. Classes named "X" aren't real examples.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please, when posting, spread the code out. A getXXX method written as one line is very difficult to read.
 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
In this case you should avoid that short-cut and try to express your question in a way which preserves the distinction between variables and objects.


I see that it might be difficult to get a clear depiction of what I am trying to say because I have intermixed the terms, from henceforth I shall maintain a clear distinction between the technical terms.


Paul Clapham wrote:
So it would be easier to answer your question if you had a real example of what you wanted to do. Classes named "X" aren't real examples.


I will do just that:

In the following example (which has been compiled to work):


I'm trying to access the textArea object from my EventHandler object, this works because textArea is an inner class and it can access it's parent's members. In this example, I get the text from the text area and then append it and then print it back out into the text area.

Now in this example:


I would like you to tell me how to get this program to compile, In an Object oriented manner. Based on how you ask me to fix this, I will inquire further if I have any clarifications
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that's a much better question.

So you have an Editor2 object, and in its constructor you have a reference to an EventHandler object and a reference to a JTextArea object. Your problem is that the EventHandler object also needs to have a reference to that JTextArea object, right?

So the first step is to add a variable (an instance variable) in EventHandler to store that object reference. The second step is to provide a way for it to get that reference: that would be a method called something like "setJTextArea" which takes the reference as a parameter. The method would assign the parameter to the instance variable. And the final step is for your Editor2 object to call that method in its constructor, thereby passing the reference from the Editor2 object to the EventHandler object.

This is the standard OOD (Object-Oriented Design) way of allowing one object to refer to another object: the controller component will pass those object references to other objects which need them, using suitably designed methods.
 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
So the first step is to add a variable (an instance variable) in EventHandler to store that object reference. The second step is to provide a way for it to get that reference: that would be a method called something like "setJTextArea" which takes the reference as a parameter. The method would assign the parameter to the instance variable. And the final step is for your Editor2 object to call that method in its constructor, thereby passing the reference from the Editor2 object to the EventHandler object.


I completely understand what you are saying:


and changing the Base classe's constructor:


So that's good, but my worry is, what if I have like 10 different types of listeners? A key listener, a mouse listener, a caret listener etc and I also have like 10 members to share, say I also added a checkbox and a menu and I have to pass those members to the concerned listeners, the code will quickly become very very long won't it? Or is that part of the drill and I am just over thinking it?
 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, could you also point me in the right direction, if I have a need to run a method on the first class?

The editor2 class get's a new method, say for example:


and I want to run it from the EventHandler class:


But in this case, the editor variable itself is unavailable, from what you have taught me, do I pass the reference to the editor object in the constructor, that way I can access both textArea as well as it's respective methods? (if that's the case, I think I solved my question I just asked above)

I'm sorry if this is bothering you, but I really want to understand this once and for all

Full source code in case you don't want to add them yourself:


Thanks
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that's good, but my worry is, what if I have like 10 different types of listeners? A key listener, a mouse listener, a caret listener etc and I also have like 10 members to share, say I also added a checkbox and a menu and I have to pass those members to the concerned listeners, the code will quickly become very very long won't it? Or is that part of the drill and I am just over thinking it?


Long code in GUIs is the norm. But you're doing it the right way to have separate classes or a utility class that holds the listeners. The listeners may become classes if they get long, but the important thing is to have a structure that easily allows you to grow and still be understandable to another programmer.

 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, could you also point me in the right direction, if I have a need to run a method on the first class?


It's been a long time since to wrote a stand-alone GUI, so I may be out of date, but my idea was this: I want to display a GUI first, so:



Does that make sense? Sheriffs, Bartenders, et al: se there a better way?
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Us sheriffs and bartenders don't necessarily know more than anybody else.

In my opinion just about every Listener should be a class on its own. (And once we are onto Listener classes we are so deep into GUI‑related territory that I shall move this discussion to our GUI‑related forum). Example: a Colour Listener. You want to change the colour of a Component. What I would do is think that sort of thing might be of general applicability, so it merits a class of its own, and I shall put it into a package of its own.And

If you do the action in one place and there is nothing else which does something remotely similar you can consider an anonymous class. In Java8 you can get rid of most of the body of an anonymous class and you get something like this
myButton.addActionListener(ActionEvent)->foo());
Now somebody, please tell them what I have got wrong in that syntax.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct way to start a Swing app is in the Event Despatch Thread. You can read about it in the Java Tutorials. In brief you write this sort of thing
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find out how to write Actions. They are better than action listeners. Find out about the AbstractAction class. It is easy to find, being the very first class in the API.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably write
SingUtilities.invokeLater(()->new GUIStarter().start());
in Java8. It won't compile until somebody tells us what I got wrong there
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SwingUtilities, not SingUtilities...
 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point of this example was not to understand events/actions or anything of that type, I want to know how I should share members or methods across multiple classes. I still am stuck at the point where one class is trying to access the other class's method

 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lokesh Poovaragan wrote:I still am stuck at the point where one class is trying to access the other class's method.


It's not quite like that. Typically you want to execute an object's method, not a class's method (unless it's a static method). And to do that you need a reference to the object. We've already discussed how to pass object references around, though, so I'm not sure exactly what your problem is now.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:SwingUtilities, not SingUtilities...
I must have been thinking about our next concert

If that is the only misstake, I am quite relieved.
 
Lokesh Poovaragan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to run the printTextArea method from the EventHandler object. But the printTextArea method exists on the Editor2 class, not on the EventHandler class, so how do I get access to it? You've asked me to pass the reference to the object and then set it using a constructor, but I cannot do that in this example:



Basically my problem is that I cannot pass an object's reference to another object because I keep getting a NullPointerException.

If you can tell me how I should structure my code to make this compile, I'll try doing it myself and ask about any queries as they arise
 
Knute Snortum
Sheriff
Posts: 4209
123
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my example I used instance variables, will these work for you?
 
Paul Clapham
Sheriff
Posts: 22713
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lokesh Poovaragan wrote:I want to run the printTextArea method from the EventHandler object. But the printTextArea method exists on the Editor2 class, not on the EventHandler class, so how do I get access to it? You've asked me to pass the reference to the object and then set it using a constructor, but I cannot do that in this example:


Well, sure you can. But really, why isn't the printTextArea method part of the EventHandler class, if that's where you're going to use it? Or why don't you have a class which contains the JTextArea and the code which uses it, like the event handler? That class could expose the JTextArea for use by your Editor2 object. When you're doing OOD it's worthwhile spending some time in deciding what's going to be an object.
 
Winston Gutkowski
Bartender
Posts: 10574
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lokesh Poovaragan wrote:Basically my problem is that I cannot pass an object's reference to another object because I keep getting a NullPointerException.

I'm no GUI expert, but that statement doesn't compute. A NullPointerException is far more likely to arise because you didn't pass a reference, not because you can't. Indeed, NPE's almost always occur because a reference was passed, but it just happened to be null at the time.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!