How do I get a variable in another method?

Asked

Viewed 106 times

3

I declared a variable within the class Main and I want to use it within a method, as I put it there?

public class Main {
   static Scanner entrada = new Scanner(System.in);
   Aluno aluno = new Aluno();
   String[] disciplinas = new String[5];
   int numeroDisciplinas = 0;

 public static void cadastrarDisciplina(){
        numeroDisciplinas +1;
        }
    }
}
  • Tried getters & setters ? https://stackoverflow.com/questions/1028967/simple-getter-setter-comments

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

2 answers

3


It has two strategies and depends on what you want.

You must communicate the value through the method, step it as argument and I receive as parameter and return the changed value for who called. Something like that:

public class Main {
    static Scanner entrada = new Scanner(System.in);
    Aluno aluno = new Aluno();
    String[] disciplinas = new String[5];
    int numeroDisciplinas = 0;
    numeroDisciplinas = cadastrarDisciplina(numeroDisciplinas);
}

public static int cadastrarDisciplina(int numeroDisciplinas) {
    return numeroDisciplinas + 1; //espero que faça mais que isto, não faz sentido só ter essa linha
}

To be honest with you, this code doesn’t seem the most appropriate, but the whole code doesn’t seem the most appropriate. Then it may be wanted to create this variable outside of the methods and it having the scope of the class all methods can access it. But if you do it with one variable, you do it with the others, right?

static int numeroDisciplinas = 0;
static Scanner entrada = new Scanner(System.in);
static String[] disciplinas = new String[5];

public class Main {
    Aluno aluno = new Aluno(); //espero que isto seja usado adequadamente, quem sabe deva estar fora também
    cadastrarDisciplina(numeroDisciplinas);
}

public static void cadastrarDisciplina() {
    numeroDisciplinas + 1;
}

I put in the Github for future reference.

It seems more appropriate, right? But I wouldn’t go so fast, it works, but in real code you’d hardly do that and you’re learning wrong. Of course, understanding the problem better may be until it can be useful in another scenario.

I still think all the code is bad and is just trying to get a problem because this code is bad, if you do it in a better way you wouldn’t have this problem. You can’t help more than that because you’d need to understand every detail of the problem. In general, people who are learning to program tend to have more problems understanding the problem than the programming itself. Focus on this.

  • Thanks for the help, the idea of my code is to use the numberDisciplines to limit the registration when filling the 5 spaces of the vector, I cut a piece of code to leave only doubt in itself, but I think this will not solve my problem, i want to run the registerDiscipline normally and whenever it is executed, add +1 in the numberDiscipline until the if stop, look only https://pastebin.com/rbz8jPyX

0

You can pass the variable to the function cadastrarDisciplina by means of a parameter.

"a parameter is a value, coming from a variable or a more complex expression, which can be passed to a subroutine." Parameter (computer science)

In your role you must pass numeroDisciplinas and return the value of this variable + 1, so you take the number of disciplines plus one.

public static int cadastrarDisciplina(int numeroDisciplinas){
        return numeroDisciplinas + 1;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.