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.
Articoli simili
- What are the two ways of invoking a method in Java?
- Qual è la differenza tra CBS All Access attraverso Amazon e CBS All Access?
- How to use the variable of one class into another class using Java
- Qual è il miglior programma di trascinamento delle onde cerebrali? Hai usato Holosync, Morry Method, Binaural Beats, ecc.