JavaRush /Java Blog /Random EN /Nested, local and anonymous classes
WhatNick
Level 17

Nested, local and anonymous classes

Published in the Random EN group
Good day to all! I began to dig deeper into the types of classes and wrote the code that contains the class. This class contains a method main()and a static nested class. A static nested class contains a method. The method creates a local class. The local class has a method that creates an anonymous class of the framing class (I strongly doubt that such perversions take place in programming practice, but for the sake of purely sporting interest I decided to be clever, so please don’t throw stones. Before the time, in any case). Simplified it looks like this: Nested, local and anonymous classes - 1
class Outer { // ОБРАМЛЯЮЩИЙ КЛАСС
    static class Inner { // СТАТИЧЕСКИЙ ВЛОЖЕННЫЙ КЛАСС
        int doInnerThings() { // ЛОКАЛЬНЫЙ МЕТОД ВНУТРЕННЕГО КЛАССА
            class LocalInner { // ВНУТРЕННИЙ ЛОКАЛЬНЫЙ КЛАСС
                int doLocalInnerThings() { // МЕТОД ЛОКАЛЬНОГО КЛАССА
                    new Outer(5) {}; // ВНУТРЕННИЙ ЛОКАЛЬНЫЙ АНОНИМНЫЙ КЛАСС
                }
            }
        }
    }
}
Goal: create a variable in an anonymous class using a framing super class, and “throw” the argument of this variable all the way to the inner class, assigning it to the inner class variable, respectively. Below is a slightly expanded code, with my sketches.
class Outer { // ОБРАМЛЯЮЩИЙ КЛАСС
    int outerValue = 0;

    Outer(int a)
    {
        this.outerValue = a*a;
    }

    int getValue() {
        return outerValue;
    }

    static class Inner { // ВНУТРЕННИЙ СТАТИЧЕСКИЙ КЛАСС
        public  int innerValue = doInnerThings();

        int doInnerThings() {
            final int innerValue = 0; // КАК ПЕРЕДАТЬ ЗНАЧЕНИЕ ЭТОЙ ПЕРЕМЕННОЙ ИЗ КЛАССА НИЖЕ?

            class LocalInner { // ЛОКАЛЬНЫЙ КЛАСС
                final public int localInnerValue;

                LocalInner() {
                    localInnerValue = doLocalInnerThings();
                }

                int doLocalInnerThings() {
                    final int[] localInnerAnonValue = {0};

                    new Outer(5) { // АНОНИМНЫЙ КЛАСС
                        @Override
                        int getValue()
                        {
                            localInnerAnonValue[0] = super.getValue();
                            return localInnerAnonValue[0];
                        }
                    };

                    return localInnerAnonValue[0];
                }

                int getlocalInnerValue() {
                    return this.localInnerValue;
                }
            }

            return innerValue;
        }
    }

    public static void main(String[] args) {
        Outer.Inner test = new Outer.Inner();
        System.out.println(test.innerValue);
    }
}
Problem: method int doInnerThings()I can’t assign innerValuethe value of a variable localInnerValue“ from a local class to a variable of this method LocalInner. I would be glad to help! :)
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION