In connection with a certain architecture, I cannot get an instance of the class in which the variable is initialized. I can declare this variable as static. But are there any other ways?

From this class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback , a stream is created MainThread(getHolder(), this); the variable is initialized in the thread. In the class public class Chronometer , which is not inherited from anything, I cannot get a stream instance, since there is no getHolder (). How to get access to a variable through a static or to use the interface or are there any other more appropriate ways? The variable itself stores time in nanoseconds and changes quite rarely.

 public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback { @Override public void surfaceCreated(SurfaceHolder holder) { Log.d(TAG,"surfaceCreated"); thread = new MainThread(getHolder(), this); thread.setRunning(true); thread.start(); } } 

.

 class MainThread extends Thread { static int start; } 

.

 public class Chronometer { start = 123; //Какими способами я могу обратиться к этой переменной ,если ее объявить не как статик? Экземпляр класса MainThread создать не могу. } 
  • How many instances of this class are created? - Artem Konovalov
  • it's pretty hard to figure out what your architecture is. add more code. - Mikhail Vaysman
  • Artem Konovalov, MainThread is a class inherited from Tread and I create one of its copies. - Turalllb
  • Mikhail Vaysman, added the code necessary to understand the question. In principle, in words, I, too, I think, explained quite clearly, but that no one understood me ... - Turalllb
  • one
    Why should the chronometer know about some variable? If this is a timestamp of reference, transfer it to the Chronometer designer. - vp_arth

1 answer 1

Need to throw an object out of a stream? As the simplest solution java.util.concurrent.Exchanger If I understood the problem correctly.

  • did not quite understand what it means to "throw out." The stream declares a variable, access to which I must get from another class in order to assign a value to this variable. The question is how can I do this. If the variable is declared as static, there are no problems with the conversion. But is it correct to use static for this purpose? Usually, in order to get access to a variable from another class, I create an instance of this class and appeal to it, but in this case it is impossible to create an instance of the class. - Turalllb
  • one
    You have 2 threads that use 1 variable. In order for both threads to have links to this variable, it is necessary either to initialize it before starting the threads and pass the link to the stream (which you actually do when declaring the static variable, you can simply declare it as an object variable, not a class, before the threads). Or transfer during execution, for this Exchanger. - Tachkin
  • and to “throw out” this I meant to convey out of the thread - Tachkin
  • one
    @Turalllb To be honest, I believe that the declaration of global ( static ) variables is the collapse of the application design OOP. Judging by the questions, there are gaps in understanding the concept of objects, I would advise you to read something about the PLO. For beginners, Head First Object-Oriented Analysis is very good. The static variable is a class variable, not an object, therefore it is initialized when the class is loaded into the perm-gen classloader. - Tachkin
  • one
    Threads are equivalent since their creation and launch (if not demons, of course). perm-gen - jvm memory area, classloader - object that is engaged in loading classes ("in perm-gen classloader" this is "where to by whom") - Tachkin