天天看点

No enclosing instance of type TestGson is accessible. Must qualify the allocation with an enclosing instance of type TestGson (e.g. x.new A() where x

main方法中实例化内部类报错:

public class TestGson {

public static void main(String[] args) {

Gson gson=new Gson();

TestGson testgson=new TestGson();

Student student = testgson.new Student();

student.setId(2);

student.setName("金旭东");

String strstudent = gson.toJson(student);

Object students = JSONObject.toJSON(student);

System.out.println("gosn="+strstudent+"fastjson="+students.toString());

}

class Student{

private int id;

private String name;

public int getId() {

return id;

public void setId(int id) {

this.id = id;

public String getName() {

return name;

public void setName(String name) {

this.name = name;