Make a copy of the class made in the if statement accessible outside this statement (Java)

First, sorry, if I do not use the correct words about making a copy of the class, I am new to Java.

System.out.println("Choose a class:\nWarrior\nRouge\nCleric\nWizard\nArcher"); String playerClass = input.nextLine(); if (playerClass.equalsIgnoreCase("Warrior")){ playerClass = "warrior"; Character player = new Warrior(); } if (playerClass.equalsIgnoreCase("Rouge")){ playerClass = "rouge"; Character player = new Rouge(); } if (playerClass.equalsIgnoreCase("Cleric")){ playerClass = "cleric"; Character player = new Cleric(); } if (playerClass.equalsIgnoreCase("Wizard")){ playerClass = "wizard"; Character player = new Wizard(); } if (playerClass.equalsIgnoreCase("Archer")){ playerClass = "archer"; Character player = new Archer(); } 

The above code is where my problem comes up. I am creating an old style adventure game and I have a class called Character . These are subclasses - these are the classes that a player can choose (wizard, warrior, blush, etc.). I know that the class is declared (again, not sure that I am using the correct word) in the if statement cannot be accessed outside the if statement, but if I do not declare it in the if statement, the compiler will not know which subclass to use for this class.

Is there a way to make classes available outside of if statements, or a way to declare a class outside of if , and then change the subclass in if statements?

+4
source share
2 answers

Just declare the variable outside the if and assign the variable inside the if . This is the same as using the playerClass variable

Like this:

 String playerClass = input.nextLine(); Character player = null; if (playerClass.equalsIgnoreCase("Warrior")){ playerClass = "warrior"; player = new Warrior(); } if (playerClass.equalsIgnoreCase("Rouge")){ playerClass = "rouge"; player = new Rouge(); } 

It is assumed that Warrior , Rouge , ets are all subclasses of Character .

+4
source

Object Instances

You do not create copies of created instances . The semantics of copy is to create a completely separate but identical instance pre-existing instance . This is also called clone in Java incorrectly due to the semantics of the term clone .

Idiomatic creation

 final Character character; // assuming that all the instances subclass `Character` if ("warrior".equalsIgnoresCase(playerClass)) { character= new Warrior(); } else if ("cleric".equalsIgnoresCase(playerClass)) { character = new Cleric(); } else if ( /* you get the idea */ ) { character= new Whatever();} else() { throw new RuntimeException(playerClass + " is not a valid class"); } 

Learning how to use final is paramount to writing, easy to maintain deterministic code.

It is important that all if/else if/else are exclusive, so there are no cases that you missed. It is never that character was null or was not correctly initialized with this code.

Idiom "SomeString".equalsIgnoreCase(variable); comparing a String literal with a variable ensures that you never have to test null and will not get a NullPointerException during the comparison, because null already inside the method.

It also avoids the use of null , which almost never works and leads to a large number of security programs and it is difficult to maintain code. You either create a valid instance, or you fail, and know exactly why this failed.

Having a character instance named player confused.

It must be a Factory method template.

 public Character characterFromClass(@Nonnull final String characterClass) { if ("warrior".equalsIgnoreCase(characterClass)) { return new Warrior(); } else if ("cleric".equalsIgnoreCase(characterClass)) { return = new Cleric(); } else if ( /* you get the idea */ ) { return new Whatever();} else() { throw new RuntimeException(characterClass+ " is not a valid class"); } } final Character character = characterFromClass(input.nextLine()); 

This is just one of the Creation templates available to you.

+2
source

All Articles