Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

you initialize the class RacingDrivers

and then call

RacingDrivers  rc = new RacingDrivers();

rc.populateMap();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top