How can i access an object from another method in java?

前端 未结 3 889
孤城傲影
孤城傲影 2020-12-10 17:08

I have the object numberlist that i created in create() method and i want to access it so i can use it in the question() method.

Is there another way to do this that

3条回答
  •  没有蜡笔的小新
    2020-12-10 17:47

    You would have to make it a class variable. Instead of defining and initializing it in the create() function, define it in the class and initialize it in the create() function.

    public class SomeClass {
        NumberList numberlist; // Definition
        ....
    

    Then in your create() function just say:

    numberlist= new NumberList(length, offset);  // Initialization
    

提交回复
热议问题