[Solved] cannot be resolved to variable


As others have stated, this site is not a school for learning the basics of the language. But sooner or later people will propably come here in search for an answer to your exact question.

You have a huge misunderstand of how Java works. In your code, you are making two mistakes. First, you try to acess a variable that would be already gone, if you could acess it. And second, you try to access a variable you can’t “see” from where you are calling. Your code:

public static void main(String[] args) {
  operators.getMonthNumber("January");
  System.out.println(monthNumber);
} 

More info on first problem:
You declare the variable monthNumber in your method getMonthNumber. So it only exists for the duration of the method. So when calling the method, the variable is there. After the method, the variable is gone. It gets thrown away. But a copy of the value of the variable is returned in your method. So you can take the copy and put it into a new variable and use it or use it directly. That’s what you pointed out with

int returnedMonthNumber=operators.getMonthNumber(“January”);

There is no programming language i know of that works the way you expect it to work. And if there was one, it wouldnt be really popular because this could cause a lot of bugs because a typo could lead to the program using a completely different variable you didnt want to use.

More info on second problem:
Generally speaking, variables are only accessible within brackets. That means, variables declared within {} are only accessible within the same brackets, or brackets within those brackets. Example:

public static void main(String[] args) { // first bracket

    final int dollar = 2;
    System.out.println("i can access dollar: " + dollar);

    {
        final int euro = 33;
        System.out.println("I can access dollar: " + dollar + ", and i can access euro: " + euro);
    }

    // you have to remove +euro to compile this:
    System.out.println("But i cant access euro because i am outside the brackets where euro is declared: " + euro);
}

Keep this rule in mind as it always works. It works with methods, with ifs, with while/for/do while etc. If the compiler can’t see a variable, it’s propably a typo or the wrong block.

I have seen a lot of beginners saying something like you said. They wonder “why won’t the compiler know what i want? It’s obvious!”. But it’s actually not obvious. The behaviour you expect is only “useful” in your head. It might save you half a line of code. But in any more advanced example you will see it would be a really bad idea to implement it the way you expect it to be. And every beginner realizes this at some point:
If you wonder why the compiler won’t do something you want it to do, then you just didn’t tell him to do what you want it to do. If you know what i mean.

solved cannot be resolved to variable