How to access an object from another method in Java
- Answering based on the example question mentioned in the above StackOverflow link.
- private static NumberList numberlist;
- private static void create() {
- Scanner input = new Scanner(System.in);
- int length,offset;
- System.out.print("Input the size of the numbers : ");
- length = input.nextInt();
- System.out.print("Input the Offset : ");
- offset = input.nextInt();
- numberlist = new NumberList(length, offset);
- }
- private static void question(){
- Scanner input = new Scanner(System.in);
- System.out.print("Please enter a command or type ?: ");
- String c = input.nextLine();
- if (c.equals("a")){
- create();
- }else if(c.equals("b")){
- numberlist.flip(); \\ error
- }else if(c.equals("c")){
- numberlist.shuffle(); \\ error
- }else if(c.equals("d")){
- numberlist.printInfo(); \\ error
- }
- }
- Making the NumberList instance a global variable may help you.
Artigos semelhantes
- What are the two ways of invoking a method in Java?
- Qual a diferença entre a CBS All Access through Amazon e a CBS All Access?
- How to use the variable of one class into another class using Java
- Qual é o melhor programa de arrastamento de ondas cerebrais? Já usou Holosync, Morry Method, Binaural Beats, etc.?