CodeGym /Courses /Java Multithreading /Constructors/variables of anonymous inner classes, and fi...

Constructors/variables of anonymous inner classes, and final

Java Multithreading
Level 4 , Lesson 8
Available

"Hi, Amigo!"

"As you already know, anonymous inner classes can't have a constructor."

"Yeah. It's not very convenient. Constructors are really important."

"As the saying goes, nothing's impossible if you want it bad enough."

"Remember, static variables don't have static constructors, but there is a static initializer – the static block."

"Yeah, I remember."

"Anonymous inner classes have the exact same initializer, except it's not static."

Example
class Car {
 public ArrayListcreatePoliceCars(int count) { ArrayList result = new ArrayList(); for(int i = 0; i < count; i++) { final int number = i; result.add(new Car() { int policeNumber; { policeNumber = number; } }); } return result; }
}

"This time I highlighted the anonymous inner class code in red, and its initializer (in effect, its constructor) in purple. The 'constructor' has a body, but there's no method signature:"

Expectation Reality
class Car { int policeNumber;
 Car(){ policeNumber = number; }
}
class Car { int policeNumber;
{ policeNumber = number; }
}

"You can declare variables inside such a class and initialize them in the initializer."

"Excellent, now their are far fewer restrictions."

"With that, we're getting to know inner classes. I hope you liked it."

"A lot. The lessons from Kim, Rishi, and especially you, Ellie, have been simply top notch."

"What a sweet talker! Keep going…"

Comments (6)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Vadim “迪姆哥” Level 35, Kazakhstan
12 September 2023
To make story short: any code that you wrote in so-called "initializer" will be moved by compiler to the default constructor of anonymous class. For example my anonymous inner class looks like this:

    public Item getSecond(){
        return new First(22.11){
            private int count;
            @Override
            public String toString() {
                return "test";
            }
            {
                this.count = 55;
            }
        };
    }
Compiler made it like this:

class First$1 extends First {
    private int count;

    First$1(First this$0, double price) {
        super(price);
        this.this$0 = this$0;
        this.count = 55;
    }

    public String toString() {
        return "test";
    }
}
Kubańczyk92 Level 35, Wroclaw, Poland
7 November 2022

{
 public ArrayListcreatePoliceCars(int count)
 {
seems like there is a missing space in the methods name
ジョーンズJ Level 47, United States
20 January 2022
"A lot. The lessons from Kim, Rishi, and especially you, Ellie, have been simply top notch." "What a sweet talker! Keep going…" 色を変更する必要があります。最初の色は青、次に2番目の色は白です。
John Squirrels Level 41, San Francisco, Poland
21 January 2022
Fixed.
Nouser Level 36, Germany
17 October 2020
or just write (as we already know)

int policeNumber = number;
Hossein Shams Level 1, Atlanta, United States
21 November 2019
I still don't know inner classes' usage in real-world programs~