6

I got the No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing instance of type test error with Location ob1 = new Location(10.0, 20.0); I'm not sure why..

package pkg;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Location ob1 = new Location(10.0, 20.0);
        Location ob2 = new Location(5.0, 30.0);
        ob1.show();
        ob2.show();
        ob1 = ob1.plus(ob2);
        ob1.show();
        return;
    }

    public class Location // an ADT
    {
        private double longitude, latitude;

        public Location(double lg, double lt) {
            longitude = lg;
            latitude = lt;
        }

        public void show() {
            System.out.println(longitude + " " + latitude);
        }

        public Location plus(Location op2) {
            Location temp = new Location(0.0, 0.0);
            temp.longitude = op2.longitude + this.longitude;
            temp.latitude = op2.latitude + this.latitude;
            return temp;
        }
    }
}
user133466
  • 3,391
  • 18
  • 63
  • 92
  • 1
    Check this post - http://stackoverflow.com/q/70324/1679863 – Rohit Jain Oct 02 '13 at 16:21
  • Did you intend to create `Location` as a nested class? – pamphlet Oct 02 '13 at 16:22
  • 1
    I would suggest you to move the `Location` class outside `test`. It doesn't seem to be related. Was it intendend? – Rohit Jain Oct 02 '13 at 16:24
  • 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 03 '16 at 23:22
  • For testing and learning purposes I was having the same scenario like you and one of the solutions is to make the classes in separate files. However if you still want want an inner class like this - you should make it static. – Combine Dec 23 '19 at 18:29

4 Answers4

11

try

Location ob1 = new test().new Location(10.0, 20.0);
Location ob2 = new test().new Location(5.0, 30.0);

you need to create an instance of outer class first, then you can create an instance of inner class

upog
  • 4,965
  • 8
  • 42
  • 81
  • 1
    Wouldn't it be easier to make `Location` into a static nested class?? – Boris the Spider Oct 02 '13 at 16:23
  • That may work around the problem, but probably isn't the solution OP needs. – pamphlet Oct 02 '13 at 16:23
  • moving location outside test (or) creating it as static will be better, I want him to understand why he got the error he mentioned – upog Oct 02 '13 at 16:26
  • 1
    The way `Location` is written, it should be a static nested class, since it appears to stand on its own and doesn't make any references to any `test` object. The only reasons I can think of to make it an inner class are (1) he's planning to add things later that _will_ refer to `test` objects, or (2) a teacher told him he had to. (And yes, learning how to handle inner classes is important.) – ajb Oct 02 '13 at 16:35
7

You may consider splitting them into 2 files. It appears that your intention is not to create nested classes, but rather having a tester class calling your core class.

File #1: Test.java

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Location ob1 = new Location(10.0, 20.0);
        Location ob2 = new Location(5.0, 30.0);
        ob1.show();
        ob2.show();
        ob1 = ob1.plus(ob2);
        ob1.show();
        return;
    }
 }

File #2: Location.java

public class Location // an ADT
{
    private double longitude, latitude;

    public Location(double lg, double lt) {
        longitude = lg;
        latitude = lt;
    }

    public void show() {
        System.out.println(longitude + " " + latitude);
    }

    public Location plus(Location op2) {
        Location temp = new Location(0.0, 0.0);
        temp.longitude = op2.longitude + this.longitude;
        temp.latitude = op2.latitude + this.latitude;
        return temp;
    }
}

When you have multiple classes defined inside a single java file, you end up creating dependencies between them, thus you're getting the error "enclosing instance of type". In your code, Test is enclosing Location. These are nested classes and unless you have good design reasons to write your classes that way, it's still best to stick to the 1-file to 1-class approach.

Jops
  • 22,535
  • 13
  • 46
  • 63
  • 1
    Might be worth noticing that an inner class can still be declared static, in which case Location could still be a (static) inner class of test and things would still work. When Location is moved outside any classes, it is considered static by default. – user118967 May 27 '19 at 07:54
0

I am very new to java, but I found the solution to this problem. The thing is wrong enclosion of braces.

package pkg;

public class test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Location ob1 = new Location(10.0, 20.0);
    Location ob2 = new Location(5.0, 30.0);
    ob1.show();
    ob2.show();
    ob1 = ob1.plus(ob2);
    ob1.show();
    return;
}
}

public class Location // an ADT
{
    private double longitude, latitude;

    public Location(double lg, double lt) {
        longitude = lg;
        latitude = lt;
    }

    public void show() {
        System.out.println(longitude + " " + latitude);
    }

    public Location plus(Location op2) {
        Location temp = new Location(0.0, 0.0);
        temp.longitude = op2.longitude + this.longitude;
        temp.latitude = op2.latitude + this.latitude;
        return temp;
    }
}
  • The braces were not quite the problem. In the original post, Location was defined as an inner class of test without being marked static. Non-static members of a class must be used with respect to an instance of that class. For example, if I have a class Foo with static method print, I can invoke Foo.print(), but if the method is not static, then I must invoke: Foo f = new Foo(); f.print(); So Location cannot be used inside main without an instance of test. When you move Location out of test, then Location is not defined inside any other classes and is considered static by default. – user118967 May 27 '19 at 07:50
0

There are several solutions you can do to fix the error:

  1. As already mentioned - make the two classes in separate files

  2. Make your inner class also static

        public static class Location { ... }
    
  3. Assuming this is only for test/learn purposes and you want to fix the error - you can call the 'Location' class like that. Mentioning this case because this illustrates the root of the error - if the class is not-static like in the original question - you have to make an object from the parent class in order to access the inner class. However fix 1,2 are better unless you have a special reason write it this way.

    Location ob1 = new test().new Location(10.0, 20.0);
    Location ob2 = new test().new Location(5.0, 30.0);
    
Combine
  • 3,894
  • 2
  • 27
  • 30