Domanda

Im trying to populate a map that has been declared as an instance variable but nothing happens when i execute the code.

Here is my class (very basic/early)

public class RacingDrivers
{
private Map<String, String> racingNumbers;

/**
* Constructor for objects of class RacingDrivers
*/
public RacingDrivers()
{
   racingNumbers = new HashMap<String, String>();
}

/**
* A method to populate the racingNumbers map.
* 
* 
*/
public void populateMap()
{
  racingNumbers.put("44", "Lewis Hamilton");
  etc etc

}

Then i execute this code:

RacingClub RC = new RacingClub();
RC.populateMap();

RC is created but the map is not populated?

È stato utile?

Soluzione

You're creating an instance of class RacingClub.

The racingNumbers Map is an instance field of class RacingDrivers.

The Map is initialized in the constructor of the RacingDrivers class.

Altri suggerimenti

you initialize the class RacingDrivers

and then call

RacingDrivers  rc = new RacingDrivers();

rc.populateMap();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top