QNA > H > How To Access An Object From Another Method In Java

How to access an object from another method in Java

  1. Answering based on the example question mentioned in the above StackOverflow link. 
  2.  
  3. private static NumberList numberlist; 
  4. private static void create() { 
  5. Scanner input = new Scanner(System.in); 
  6.  
  7. int length,offset; 
  8.  
  9. System.out.print("Input the size of the numbers : "); 
  10. length = input.nextInt(); 
  11.  
  12. System.out.print("Input the Offset : "); 
  13. offset = input.nextInt(); 
  14.  
  15. numberlist = new NumberList(length, offset); 
  16.  
  17. private static void question(){ 
  18. Scanner input = new Scanner(System.in); 
  19.  
  20. System.out.print("Please enter a command or type ?: "); 
  21. String c = input.nextLine(); 
  22.  
  23. if (c.equals("a")){  
  24. create();  
  25. }else if(c.equals("b")){ 
  26. numberlist.flip(); \\ error 
  27. }else if(c.equals("c")){ 
  28. numberlist.shuffle(); \\ error 
  29. }else if(c.equals("d")){ 
  30. numberlist.printInfo(); \\ error 
  31.  
  32. Making the NumberList instance a global variable may help you. 

Di Farah Hannay

How to use the variable of one class into another class using Java :: How to use an array of objects and call them in methods in Java
Link utili