MikeNeto
MikeNeto

Reputation: 3

Edit Classes in Runtime

My program read's a file with 5 parameters about a unit. I have established a unit class with these parameters however it was asked now to be able to read another file, this one had 6 parameters but it got me thinking if i could get a file with 10+ parameters and my unit class would not be ready to store all that data, so i was wondering if i could add more variables to a class in runtime.

Sample

Unit Class

public class Unit implements Serializable {

    private String name;
    private String unitId;
    private byte year;
    private String semester;
    private String type;
    private int credits;

    public Unit(String name, String unitId, byte year, String semester, int credits) {
        setName(name);
        setUnitId(unitId);
        setYear(year);
        setSemester(semester);
        setType(null);
        setCredits(credits);
    }

    public Unit(String name, String unitId, byte year, String semester, String type, int credits) {
        setName(name);
        setUnitId(unitId);
        setYear(year);
        setSemester(semester);
        setType(type);
        setCredits(credits);
    }
    // Set's get's and all that stuff.

}

Sample code to read the files

Scanner input = new Scanner(f);
ArrayList<Unit> units = new ArrayList();
while (input.hasNext()) {
    String str = input.nextLine();
    if (ignoreFirstLine) {
        ignoreFirstLine = false;
    } else {
        String[] ArrayStr = str.split(";");
        if(ArrayStr.length == 5){
            Unit unit = new Unit(ArrayStr[0], ArrayStr[1], Byte.parseByte(ArrayStr[2]), ArrayStr[3], Integer.parseInt(ArrayStr[4]));
            units.add(unit);
        } else if (ArrayStr.length == 6){
            Unit unit = new Unit(ArrayStr[0], ArrayStr[1], Byte.parseByte(ArrayStr[2]), ArrayStr[3], ArrayStr[4], Integer.parseInt(ArrayStr[5]));
            units.add(unit);
        } else {
            //Modify classes in Runtime?
        }

edit: My english is amazing :D

Upvotes: 0

Views: 101

Answers (1)

user2336315
user2336315

Reputation: 16067

so i was wondering if i could add more variables to a class in runtime

No. In Java, you can't insert new variables into a compiled program. If you're not sure about how parameters(and their types) you could get, try to store them in a Collection (i.e an HashMap<Long, Object> for example).

    else {
                HashMap<Long, Object> map = new HashMap<>();
                for(int i = 6; i < ArrayStr.length; i++)
                     //add items here

                  Unit unit = new Unit(ArrayStr[0], 
                                       ArrayStr[1], 
                                       Byte.parseByte(ArrayStr[2]), 
                                       ArrayStr[3], 
                                       ArrayStr[4], 
                                       Integer.parseInt(ArrayStr[5]), 
                                       map);
                   units.add(unit);
}

Note that you will have to change your constructor.

Otherwise, you have to change your design. You could check this thread.

Upvotes: 2

Related Questions