class - How to correctly implement communication between Java classes -


i'm beginner in java programming , i'm working on app more complex class structure , gui. might stupid questions, hard google, i'm asking here.

i have main class, looking this:

package app;  public class app {     private firstclass fc;     private secondclass sc;      public app () {         fc = new firstclass ();         sc = new secondclass ();          // ... code continues ...     } } 

say secondclass defined outside of .java file (like gui forms are). there way me access "fc" instance (or other member variables of app instance) "sc" instance (without passing "this" pointer)? like:

class secondclass {     public void somemethod() {         getwhoevercreatedthisinstance().fc.getsomedata();          // ... code continues ...     } } 

and if not, doing wrong? should design differently? maybe setting "fc" static? if want more of app's classes communicate each other, should make them static? point of having non-static then? pass "this" pointer of "app" or "fc" instance in constructor of "secondclass", solution seems non-elegant when number of classes need behavior rises.

any ideas? in advance!

my suggestion implement callback system interfaces. each of classes communicating each other should implement these. classes should register creating class. can call method in creating class invokes interface method of each registered class , passed data way.

this answer might https://stackoverflow.com/a/18279545


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -