Error creating an instance of an object in an if block

I wrote a program in which users can choose between two games: Pig or Snake. I tried using the following code in my main method to create the appropriate kind of game:

if (gameType == 'p') PigGame game = new PigGame(); else SnakeGame game = new SnakeGame(); 

My compiler points to the second line and gives an error: not a statement

I managed to fix the problem by writing the abstract class "Game" and then subclassing PigGame and SnakeGame:

 Game game; if (gameType == 'p') game = new PigGame(); else game = new SnakeGame(); 

But I do not understand why the first design did not work. (I'm ready to teach a programming course in high school, but I'm new to Java and OOP, so I can use any ideas you can provide.)

+4
source share
4 answers

The problem is that the game area is inside the if and else

 if (gameType == 'p') PigGame game = new PigGame(); else SnakeGame game = new SnakeGame(); 

Thus, you cannot use it anywhere, so your second part of the code works.

+3
source

"PigGame game = new PigGame ();" it is a declaration, not an expression. If instruction is required, not a declaration. You can create a statement block containing it:

 { PigGame game = new PigGame(); } 

This will be syntactically correct. This would be pointless because the game would be local to the statement block. You need the game to be declared with a wider scope.

+2
source

This is actually a pretty important question.

 if (gameType == 'p') PigGame game = new PigGame(); else SnakeGame game = new SnakeGame(); 

does not compile just because of the Java grammar, which states that the variable declaration is not statemant. In fact, you can imagine code in which it actually does not need this β€œgame” variable and just wants to run the constructors in the if statement.

 if (gameType == 'p') new PigGame(); else new SnakeGame(); 

and this will compile

 if (gameType == 'p'){ PigGame game = new PigGame(); }else{ SnakeGame game = new SnakeGame(); } 

it will also compile

Naturally, in both cases - we do not have access to the variable outside the brackets, but nevertheless, so that the user can not encode it as

 if (gameType == 'p') PigGame game = new PigGame(); else SnakeGame game = new SnakeGame(); 

is a little arbitrary, and AFAIK aims to make sure that the user knows what he is doing. In the general case, the evaluation of a variable is determined by its declaration around the brackets, so if you write

 { Integer x=3; System.out.println(x); //this works } System.out.println(x); //but this does not 

and the same thing applies: you can declare a Game object inside an if statement, but cannot be taken outside of it by a Game variable. But it depends on your code. You might have something like

 public class Game{ public static Game lastCreatedGame; public Game(){ lastCreatedGame=this; } } 

then by running

 if (gameType == 'p') new PigGame(); else new SnakeGame(); 

still give you access to the game, in the field Game.lastCreatedGame

+1
source

That's why I ask all newcomers not to skip braces, because it gives more clarity in scope.

When you do

 if (gameType == 'p') PigGame game = new PigGame(); else SnakeGame game = new SnakeGame(); 

in

 if (gameType == 'p') { PigGame game = new PigGame(); } else { SnakeGame game = new SnakeGame(); } 

scope becomes clearer both

PigGame game = new PigGame(); and SnakeGame game = new SnakeGame();

exist only within their respective blocks (i.e.) within their respective curly braces,

therefore, you cannot use the game anywhere outside of its respective blocks, the compiler sees this and throws an error to help you.

+1
source

Source: https://habr.com/ru/post/1496381/


All Articles