Results 1 to 4 of 4
  1. #1
    DianaOli is offline Member
    Join Date
    Apr 2017
    Posts
    2
    Rep Power
    0

    Default How to use a variable from a class to another one?

    So, i have this code in a class called "menu", which allows the user to select one of the options which lead to different paths.
    Java Code:
    import java.util.Scanner;
    
    public class menu {
    
    	public static int menu(){  
    		Scanner digito= new Scanner(System.in);     
    		int numerodesejado;     
    		do{          
    			System.out.println("Menu");        
    			System.out.println("1- Gestão de quadras");      
    			System.out.println("2- Gestão de participantes");
    			System.out.println("3- Gestão de jornadas");
    			System.out.println("4- Estatísticas"); 
    			System.out.println("0-sair");         
    			numerodesejado=digito.nextInt();      
    			}
    		while(numerodesejado<0|| numerodesejado>4);
    
        return numerodesejado;    
    	} 
    	}
    And , in "main", I wanted to do this:
    Java Code:
    public class Campeonato {
    
    	public static void main(String[] args) {
    		numerodesejado = menu();
    		switch (numerodesejado){
    		case 1: 
    			break;
    		case 2: 
    			break;
    		case 3: 
    			break;
    		case 4: 
    			break;
    		case 5: 
    			break;
    			
    		}
    		}
    }
    Aka, i wanted to use the number selected by the user that would be stored in the variable "numerodesejado" in the class "campeonato" and, by using the "switch", connecting that to another class that would do what I wanted in each number picked. But i don't know how to pass a variable from a class to another one. It says ""numerodesejado" cannot be resolved to a variable" and "The method menu() is undefined for the type Campeonato".
    The language used is in Portuguese by the way. Help!! Thank you :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to use a variable from a class to another one?

    Threads merged.

    To call a static method in a class use classname dot the method name: TheClass.theMethod()

    "numerodesejado" is not recognized
    It needs to be defined somewhere in scope where it is being accessed.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DianaOli is offline Member
    Join Date
    Apr 2017
    Posts
    2
    Rep Power
    0

    Default Re: How to use a variable from a class to another one?

    Quote Originally Posted by Norm View Post
    It needs to be defined somewhere in scope where it is being accessed.
    But if I define it in where i want it to be used, then wouldn't it be the same as me creating a new variable?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to use a variable from a class to another one?

    creating a new variable?
    Yes, to use a variable, it must be defined so that it is in scope all the places where you want to access it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 14
    Last Post: 03-25-2015, 09:20 AM
  2. Replies: 2
    Last Post: 01-08-2014, 05:09 PM
  3. Replies: 5
    Last Post: 12-18-2011, 04:06 AM
  4. Replies: 0
    Last Post: 10-23-2011, 10:49 PM
  5. Replies: 5
    Last Post: 01-26-2011, 06:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •