aspose file tools*
The moose likes Beginning Java and the fly likes Global data and external classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Global data and external classes" Watch "Global data and external classes" New topic
Author

Global data and external classes

Olaf Enulfson
Greenhorn

Joined: Jun 18, 2009
Posts: 23
Hello,

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.

O.
--
There are 10 kinds of people:
Those who understand ternary.
Those who don't.
And those who confuse it with binary.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
You don't have global data; there is no such thing as a "global" value in Java, but a field used as a constant is the nearest approach available, as in this little example:As I said, that is as near as you get to "global data" in Java.

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.
Olaf Enulfson
Greenhorn

Joined: Jun 18, 2009
Posts: 23
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? :-(

O.
--
Use the source, Luke.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

Of course, it is possible, but there is no magic here -- you have to give access to the inner class (now it's own class).

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Olaf Enulfson
Greenhorn

Joined: Jun 18, 2009
Posts: 23
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"?


O.
--
Murphy was a pessimist.
Olaf Enulfson
Greenhorn

Joined: Jun 18, 2009
Posts: 23
Yeeha! It works!

Thanks Henry.
(Thanks to everyone else too).

O.
--
In order to understand recursion, one must first understand recursion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Global data and external classes