سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

you initialize the class RacingDrivers

and then call

RacingDrivers  rc = new RacingDrivers();

rc.populateMap();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top