简体   繁体   中英

How can I use an object outside of it's declared scope?

For example, if I have a class Student and an student object inside of it with the value of 5, also some getters and setters

public class Student {
    int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public static void run() {
        Student stud = new Student();
        stud.setAge(5);
    }
}

And I have another class called getStudent

public class GetStudent {
    int studAge = stud.getAge;
}

Now this, obviously, gives me an error. Is there any way to access the stud object and it's methods outside of it's class?

如果您总是只想通过public static void run()方法访问 Student 对象,请查看单例设计模式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM