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.
문제
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();