I have a class User
, only have two fields: id, name
package test;
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Then in the Main
class, I tried to initialize the User object with two different approach:
package test;
/**
* @author lhuang
*/
public class Main {
public static void main(String[] args) {
User u = new User() {
{
setId(1l);
setName("LHuang");
}
};
System.out.println(u.getClass());
User u2 = new User();
u2.setId(1l);
u2.setName("LHuang");
System.out.println(u2.getClass());
}
}
then I can get the output
class test.Main$1
class test.User
The interesting is why the u class is the inner class type of Main? But actually I still can use the u.getId()
and u.getName()
method.