Code: Select all
import java.util.*;
public class ClassnameTest {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
StringBuilder sb = new StringBuilder("mrx");
String s = sb.toString();
list.add(s);
System.out.println(s.getClass());
System.out.println(list.getClass());
}
}
class java.lang.String
class java.util.ArrayList
Process finished with exit code 0
==========
So Explanation correct, but answer in test - NOT.
"The getClass method always returns the Class object for the actual object on which the method is called irrespective of the type of the reference."