Defining custom function/property inside anonymous class

SimpleGuy :

I want to define my property and function in anonymous class as under

ExistingExtendableJavaClass aClass = new ExistingExtendableJavaClass() {
         public String someProperty;

         public String getMyProperty() { return someProperty }
});

But then these calls don't work

aClass.someProperty // not accessible
aClass.getMyProperty() // not accessible

I know because ExistingExtendableJavaClass doesn't have these, but then my anonymous has these. How can I achieve this ?

rzwitserloot :

They are accessible just fine:

new ExistingExtendable() {
    public void foo() {}
}.foo();

works great.

But if you write:

ExistingExtendable x = new ExistingExtendable() {
    public void foo() {}
};
x.foo();

That does not work. For the same reason this doesn't work:

Object o = new String();
o.toLowerCase(); // nope

The problem is that your anonymous class has no name, thus, you cannot denote its type. We can fix the string example by replacing Object o with String o, but there is no String equivalent.

However, that is the point of an anonymous inner class.

If you want these to be denotable, then you don't want an anonymous inner class. Asking: "I want an anonymous inner class, but I want the new members I declared in them to be accessible" is like asking: "I want a circle, but.. with corners".

You can make method local inner classes, and now you have names:

public void example(String x) {
    class IAmAMethodLocalClass extends ExistingExtendableJavaClass {
        String someProperty; // making them public is quite useless.

        String foo() {
            System.out.println(x); // you can access x here.
        }
    }

    IAmAMethodLocalClass hello = new IAmAMethodLocalClass();
    hello.someProperty = "It works!";
}

an anonymous inner class is the same as this method local class thing, except it avoids naming the type. In this case, you NEED that name, thus, you can't use the anonymous inner class construct.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java: Defining a generic method inside an anonymous class

From Dev

Defining functions inside class

From Dev

Defining a custom equality operator in an anonymous namespace

From Dev

who is 'this' inside an anonymous class implementation?

From Java

Is there a benefit to defining a class inside another class in Python?

From Dev

Are there any issues with defining a custom hook inside of a component?

From Dev

Defining a class inside a function to interrupt decorator execution

From Dev

Defining a templated friend function inside a template class

From Dev

Defining a class with no default constructor inside a struct

From Dev

TypeScript/React: Defining state outside or inside a class

From Dev

What is the advantage of defining an public Enum inside a class?

From Dev

Defining custom gradient as a class method in Tensorflow

From Dev

Defining the size of an array on a custom templated Array class

From Dev

What calls a method inside an anonymous inner class?

From Dev

Declare instance variable in anonymous class inside enum

From Java

Exception thrown inside an anonymous inner class not caught

From Dev

Javascript use anonymous function inside class methods

From Java

How to change instance variables inside anonymous class

From Dev

Define a QObject derived class inside an anonymous namespace?

From Dev

How to namespace constants inside anonymous class definitions?

From Dev

How to stop Swing timer inside the anonymous class?

From Dev

Retaining value of variables inside anonymous inner class

From Dev

How to access class method inside an anonymous function

From Dev

Defining sub-structure of sub-class inside parent class

From Dev

Defining a decorator inside a class to affect class variables in a Pythonic way

From Dev

How to get the parent class inside anonymous class in kotlin

From Dev

Defining the custom-element's lifecycle callbacks inside of the constructor

From Dev

Redefinition error when defining friend function inside class template

From Dev

Defining widgets inside initState or class constructors rather than build?

Related Related

  1. 1

    Java: Defining a generic method inside an anonymous class

  2. 2

    Defining functions inside class

  3. 3

    Defining a custom equality operator in an anonymous namespace

  4. 4

    who is 'this' inside an anonymous class implementation?

  5. 5

    Is there a benefit to defining a class inside another class in Python?

  6. 6

    Are there any issues with defining a custom hook inside of a component?

  7. 7

    Defining a class inside a function to interrupt decorator execution

  8. 8

    Defining a templated friend function inside a template class

  9. 9

    Defining a class with no default constructor inside a struct

  10. 10

    TypeScript/React: Defining state outside or inside a class

  11. 11

    What is the advantage of defining an public Enum inside a class?

  12. 12

    Defining custom gradient as a class method in Tensorflow

  13. 13

    Defining the size of an array on a custom templated Array class

  14. 14

    What calls a method inside an anonymous inner class?

  15. 15

    Declare instance variable in anonymous class inside enum

  16. 16

    Exception thrown inside an anonymous inner class not caught

  17. 17

    Javascript use anonymous function inside class methods

  18. 18

    How to change instance variables inside anonymous class

  19. 19

    Define a QObject derived class inside an anonymous namespace?

  20. 20

    How to namespace constants inside anonymous class definitions?

  21. 21

    How to stop Swing timer inside the anonymous class?

  22. 22

    Retaining value of variables inside anonymous inner class

  23. 23

    How to access class method inside an anonymous function

  24. 24

    Defining sub-structure of sub-class inside parent class

  25. 25

    Defining a decorator inside a class to affect class variables in a Pythonic way

  26. 26

    How to get the parent class inside anonymous class in kotlin

  27. 27

    Defining the custom-element's lifecycle callbacks inside of the constructor

  28. 28

    Redefinition error when defining friend function inside class template

  29. 29

    Defining widgets inside initState or class constructors rather than build?

HotTag

Archive