I don't know what is your programming background, so I'll assume here that you have close to none. If this isn't the case, don't take this as an insult ;)
The answer to your question really depends on what you really want. Do you want a variable name that represents what your object is, or you want a permanent name on your data that will be bound to it for the whole object's lifetime?
In the first case, then the answer is no, this isn't possible. As you have mentionned, you need to define your variable's name before you compile your code. Dynamic variable names doesn't make sense in this context, since the variable's name is only valid in its scope. Once you get outside the scope where the variable was defined, then the name doesn't exist anymore.
For example:
public void someMethod () {
SomeObject myNameIsJonas = createSomeObject ();
}
private SomeObject createSomeObject () {
SomeObject myNameIsMud = new SomeObject ();
return myNameIsMud;
}
Inside the method createSomeObject, the variable that holds the created object is called myNameIsMud. Once you exit the method and return the object, it is assigned to a variable called myNameIsJonas. So, even if you had dynamic variable names support in Java, it wouldn't really matter since the variable myNameIsMud cease to exist once you exit the method createSomeObject.
However, if what you want is some sort of permanent tag on the object, it is easily achieved. You can add a private class member inside your class (AgencyDetails), and set it either from the constructor, or from a setter method (setAgencyName (String name)). Then you can use this name as the key for a Map.
public class AgencyDetails {
private String name = "";
public AgencyDetails (String name, int number) {
this.name = name;
}
public String getName () {
return name;
}
}
public class LesClaypool {
private Map<String, AgencyDetails> agencyMap = new HashMap<String, AgencyDetails> ();
public void doWork () {
// Read agency info from file into nameFromFile and numberFromFile
AgencyDetails agency = new AgencyDetails (nameFromFile, numberFromFile);
agencyMap.put (agency.getName (), agency);
}
public AgencyDetails getAgencyDetailsByName (String name) {
return agencyMap.get (name);
}
}
After you have read the agency details information from your file, you can store them into a Map, using the name as the key. You can then get the AgencyDetails instance you are interested in by providing its name to the getAgencyDetailsByName method, as you can see with the simple example above.
Map.Mapyou'll be able to "name" each element (in fact you assign a key to each element, not a name)List, your number is the index in that list.