1

I am relatively new to working with Java and have come from a C/C++ background. In my app I am needing a number of classes which will simply hold data.

Rather than have one file per data class, I thought of simply putting all the classes inside a basic class.

Here is what I am declaring -

public class InternalData
{
    public class LocalSearchDef
    {
        public String m_sAdresse = null;
        public GeoPoint m_gpPoint = null;
        public int m_iSearchRadius = 0;
    }

    public class GeoBounds
    {
        public double m_dNELat;
        public double m_dNELng;
        public double m_dSWLat;
        public double m_dSWLng;
    }
}

However, eclipse tells me that there is "No enclosing instance of type InternalData is accessible. Must qualify the allocation with an enclosing instance of type InternalData (e.g. x.new A() where x is an instance of InternalData).", when I try to create a new instance of, say, GeoLocation I get the error.

This happens with either :

GeoLocation gl = new GeoLocation();
or
GeoLocation gl = new InternalData.GeoLocation();

Can anyone point me in the right direction please ?

Simon
  • 2,208
  • 4
  • 32
  • 47
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:26

3 Answers3

1

You would need to make the internal classes static.

public static class Foo {

}
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
  • This will fix his problem, but it's better code practice to enclose his inner classes with an instance of his parent unless he understand why static works the way it does and how to use it. – Charles Apr 30 '12 at 15:50
  • Given that his intention is simply to group some classes together as 'InternalData' there is no reason for him to ever have an instance of the external class, therefore I would say best practice would probably be to make the classes static. – DaveJohnston Apr 30 '12 at 15:54
  • @Charles - _He_ is not sure now how static works the way it does as you say - you have raised doubts. Will each class instance have its own data space, or is there a sharing of data ? – Simon Apr 30 '12 at 15:59
  • One of the, obvious, reasons for doing this was to handle project management and not have a multitude of files. I have decided to simply create a data package to hold all my data related classes. This allows me to separate better the content from the UI. Thank you for the comments. __Note__ : I wanted to put this in the answer section, but didn't want to wait 8 hours to do it. – Simon Apr 30 '12 at 16:18
1

Although you can do it using a static declaration, don't try to do it this way.

If you are going to encapsulate classes within another, they should have some relationship to the enclosing class (like Map.Entry to Map). You are simply trying to organize multiple classes into the same file by making a holder class, that serves no purpose.

There is nothing wrong with one class per file, this is the standard Java approach. You need to justify why they should be in the same file, not the other way around.

You may also want to declare your classes with accessor methods not simply create data structs with all public access to the data members. This is poor OO.

Robin
  • 24,062
  • 5
  • 49
  • 58
0

You're trying to use a non-static inner without an instance of InternalData for it to belong to.

See: An enclosing instance that contains <my reference> is required

Community
  • 1
  • 1
Charles
  • 245
  • 2
  • 11