0

I started learning Java and there was an error in line 2: "Small cannot be resolved to a type". Why?

public class Big {
public Small getSmall() {
    return new Small() {
        void out() {
            System.out.println("Big Small out()");
        }
    };
}

public static void main(String[] args) {
    Big b = new Big();
    Small s = b.getSmall();
    s.out();
    System.exit(0);
}

}

2
  • Where is small declared? An anonymous class still needs an object to extend. Commented Jun 27, 2014 at 9:30
  • Where is your Small class? Commented Jun 27, 2014 at 9:32

5 Answers 5

3
public Small getSmall() {
    return new Small() {
        void out() {
            System.out.println("Big Small out()");
        }
    };
}

There seems to be little misunderstanding. When you declare a anonymous inner class it is assumed Small is either an interface that you are implementing or class that you are extending. So if you already have Small class or interface you need to import it. If not create one.

Sign up to request clarification or add additional context in comments.

Comments

0

The error you get means that compiler could not find the declaration of Small class. Make sure you have imported this class properly, or this class has proper declaration and/or located in the same package where your main class located.

Comments

0

The syntax

return new Small() {
    void out() {
        System.out.println("Big Small out()");
    }
};

can be be imagined (roughly) as a short form of

return new SmallWithOut();

with a class like

class SmallWithOut extends Small {
    void out() {
        System.out.println("Big Small out()");
    }
}

You could also translate it into words as

Create a new instance of a class that is a subclass of the class Small (with this out() method...)

The problem is that you did not declare the class Small at all. So the class that should be extended there is simply not known.

There are several ways to get this to compile. The simplest is to add the class, locally, as an inner class (or even an interface)

public class Big
{
    private static class Small
    {
        void out() {};
    }

    ...
}

Comments

0

Anonymous Classes Extend/Implement Other Classes/Interfaces

You can't just create a class identifier from scratch using anonymous classes. Anonymous classes assume you are extending or implementing another class or interface. But you could do the following;

abstract class Small {} // Small declared here

public class Big {
    public Small getSmall() {
        // Create and return an anonymous class that extends Small.
        return new Small() {
            void out() {
                System.out.println("Big Small out()");
            }
        };
    }

    public static void main(String[] args) {
        Big b = new Big();
        Small s = b.getSmall();
        // Compile error below! Your Small reference doesn't know about out()
        s.out(); 
        System.exit(0);
    }
}   

When you create your anonymous Small class, you are creating a new (anonymous) class that extends Small. In the above example, you'll note that the abstract class Small does not have an out() method. Even though you can add that in your anonymous declaration you will never be able to call it, because you will still have to hold your new anonymous class in a Small reference variable.

Alternatively, you could just create an anonymous class that extends Object. But then it would have to be kept in an Object reference and you would only be able to call object methods from it.

I hope this helps.


Further Reading;

Comments

-1

A class inside another class and that class will not provide class name i.e that class is a Anonymous inner in java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.