4

I'm a beginner in Java programming and I'm currently working on an app with more complex class structure and a GUI. This might be a stupid questions, but it is very hard to google, so I'm asking here.

I have a main class, looking like this:

package app;

public class App {
    private FirstClass fc;
    private SecondClass sc;

    public App () {
        fc = new FirstClass ();
        sc = new SecondClass ();

        // ... code continues ...
    }
}

Say the SecondClass is defined outside of this .java file (like GUI forms are). Is there a way for me to access the "fc" instance (or other member variables of the App instance) from the "sc" instance (without passing the "this" pointer)? Something like:

class SecondClass {
    public void someMethod() {
        getWhoeverCreatedThisInstance().fc.getSomeData();

        // ... code continues ...
    }
}

And if not, what am I doing wrong? Should I design this differently? Maybe setting the "fc" as static? But what if I want more of my app's classes to communicate with each other, should I make them all static? What would be the point of having something non-static then? I could pass the "this" pointer of "App" or "fc" instance in the constructor of "SecondClass", but that solution just seems non-elegant when the number of classes that need this behavior rises.

Any ideas? Thanks in advance!

  • could you be more specific with the question? like do you want to have only a single instance of `FirstClass`? – Blip Nov 05 '16 at 17:56
  • If you don't pass `App` (aka `this`) to the `SecondClass` constructor, how did you expect `getWhoeverCreatedThisInstance()` to work, given that `App` *is* the whoever-created-this-instance? – Andreas Nov 05 '16 at 18:05
  • @Blip: yes, there will be only one instance necessary. Basically it's classes like GUI, database connector etc... – Stanley Barkwill Nov 05 '16 at 18:29
  • @Andreas: well I was wondering if Java offers some way to access the "creator" instance without the "this" pre workaround – Stanley Barkwill Nov 05 '16 at 18:30
  • @StanleyBarkwill It doesn't. And passing `this` is not a "workaround". That's how you do it. – Andreas Nov 05 '16 at 18:31
  • Generally when creating a Swing GUI, you would use the [model / view / controller pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). Read my [Moving Eyes](http://stackoverflow.com/questions/34981403/bufferedimage-not-being-cleared-before-each-rendering/35002727#35002727) answer for a simple GUI using the model / view / controller pattern. – Gilbert Le Blanc Nov 05 '16 at 20:45

2 Answers2

1

My suggestion is to implement a callback system with interfaces. Each of your classes communicating with each other should implement these. The classes should Register to the creating class. Then they can call a method in the creating class which invokes the interface method of each registered class and passed the data this way.

This SO answer might help https://stackoverflow.com/a/18279545

Community
  • 1
  • 1
S. Dragomir
  • 318
  • 2
  • 11
0

If you want to develop GUI applications, you should really get into the basic concepts. This can be very time-consuming, but it is necessary, otherwise you will encouter strange behaviour. I will just give you a basic understanding to answer your question.

You think of simple console applications, where you usually have a single thread and passing around objects is valid. With multiple threads, this is fatal, even with static variables. Each variable or object can be modified concurrently and the other thread may not be able to 'see' the changes in time. This is a complex matter, since there are also caches and separate stacks for each thread. In short, fc may not always be synchronized in App and sc, therefore reads and writes may be inconsistent.

What to do now? Learn the concepts of GUI programming. Often you do not even have to share objects for simple things. If a GUI control triggers an action, use a Listener, look here. If you want to access a database for example, then just make a new connection object for each request or button click, whatever. This is simple to start, add complexity later.

A simple variant to share objects is to use the synchronized keyword, which ensures that a method or a field is only accessed by one thread at a time. Here is an example. Also look at thread-safe data structures provided by Java (java.util.concurrent).

For advanced purposes you would have a separate thread and you would connect them with Sockets to pass messages or data.

Community
  • 1
  • 1
thatguy
  • 21,059
  • 6
  • 30
  • 40