In a project I have a class in which I have created an internal class. Multiple instances of this inner class use data defined "globally" in the outer class. But the code is getting too big to be browsable, so I'd like to make the inner class external. How can I access the global data from the inner class without passing the data to the constructor?
I wrote a smaller code example with two levels of inner classes. Ideally I'd like to split up this one file in three files.
I am rather new to Java. I assume there must be an obvious solution?
Thank you in advance for your time.
There are 10 kinds of people:
Those who understand ternary.
Those who don't.
And those who confuse it with binary.
You have a field called "global" but it is private to the outer class; all the members of the class, which includes the inner classes, have access to that field. So you should be able to print out the values of "global" and "local". What happens when you try to compile that code? What output do you get? Is it as expected?
The idea of a constructor is to set up the initial state of an object. It is not intended for "behaviour", which should be implemented in methods. If you want an object to "do" something you would have to set up a method to do it (so the switch block would move out of the constructor). If you have top-level classes, you can take information from the get methods of one and pass it as arguments to the methods of another object.
there is no such thing as a "global" value in Java, but a field used as a constant is the nearest approach available
Ok, "global" was a misnomer. But I don't mean constants either. The data in the outer object is changing, and the inner objects need to read this data regularly. Not only in the constructor. That was just for the example.
I need the inner class, written in another file, to be able to access this data defined in the calling class. There is a keyword "super" in Java, but it doesn't give me access to the data nor methods I need. I suppose that only works for inheritance, and not for embedded classes. But this kind of feature is what I am looking for.
What happens when you try to compile that code? What output do you get? Is it as expected?
The way it is written, in one big source file, it reacts as expected. But when I split up the classes in separate files, Eclipse complains that the getter methods don't exist, and need to be created locally.
This is only a cosmetic problem. I have one huge source file, and I'd like it split up per class, even though they are inner classes.
Maybe it isn't possible? :-(
Use the source, Luke.
It is probably easiest to pass the instance of the outer class to the instance of the formerly inner class -- and probably do it during construction. Then the formerly inner class object can access what it use to access directly with the reference that is passed. The next issue is, of course, permissions. You need to make sure that the formerly inner class has permission to access those fields of the out class -- probably via getters and setters.
you have to give access to the inner class (now it's own class).
Yes, and how do I do that?
It is probably easiest to pass the instance of the outer class to the instance of the formerly inner class -- and probably do it during construction. Then the formerly inner class object can access what it use to access directly with the reference that is passed.
Yes yes, that sounds great. What syntax would I need for that?
Would it be using keyword "this"?
Murphy was a pessimist.