Can outer classes call methods of their inner class?

I have code that establishes a connection to the server during an event that the user clicks on a specific button. I created an inner class to listen for the action. As part of the only method that I have in the inner class, I also establish the server connection mentioned earlier.

My question is, can a Socket connection be used only from an "inner" class? Or, can an external class continue to communicate with the specified server?


However, I understand that the inner class has unlimited access to the outer class (as if it were an outer class. My question is the other way around).

+4
source share
2 answers

Create such an instance and get access to what you want:

OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 
+7
source

All methods declared in the inner class are available ... whether they are declared as public or ... private .

If the internal methods are static , then they can always be called by code in the external class. You just need to define a method name with the name of the inner class.

Otherwise, the outer class code needs a reference for an instance of the inner class to call methods on it. (But it normal.)


(If you asked about whether the inner class can call methods in the outer class, this is a bit more complicated. In most cases this applies, but if the inner class is NOT static , it can also call instance methods on its outer class via this .)

+2
source

Source: https://habr.com/ru/post/1484539/


All Articles