ClassName of annonymous inner class is same – obj.getClass().getName()

Questions:

I am creating 20 new new inner classes and overriden a method to print the class name. But everytime i am getting those 20 instances the class name is the same. My requirement is to get different class names for each of them. How can I achieve this? Any pointers are much appreciated – thank you.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class TestClassName {
    public static void main(String[] args) {


    List  list=   IntStream.rangeClosed(1, 20).mapToObj(e -> new Check() {

        @Override
        public void execute() {
            System.out.println(this.getClass().getName());

        }
    }).collect(Collectors.toList());

list.forEach(i -> ((Check)i).execute());
}
}

interface Check {
public abstract void execute();
}

Output

TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
    TestClassName$2
Answers:



Questions:

I am creating 20 new new inner classes and overriden a method to print the class name. But everytime i am getting those 20 instances the class name is the same. My requirement is to get different class names for each of them. How can I achieve this? Any pointers are much appreciated – thank you.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class TestClassName {
    public static void main(String[] args) {


    List  list=   IntStream.rangeClosed(1, 20).mapToObj(e -> new Check() {

        @Override
        public void execute() {
            System.out.println(this.getClass().getName());

        }
    }).collect(Collectors.toList());

list.forEach(i -> ((Check)i).execute());
}
}

interface Check {
public abstract void execute();
}

Output

TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
TestClassName$2
    TestClassName$2
Answers:



Need Help? Chat with us