Vswe's Summer Courses | You're not logged in. Sign up or log in | Server time: | Donate
One can now see other persons' badge hunts. Simply head over to their profiles to access them.

Go back to sub forum page

General forum

Non-course related subjects

3. Programming help

Reference to calling object in superclass

  • 2013-08-25 11:31:57
    My problem is a bit hard ro explain so just let me show you.

    I have an abstract class called Order. Then I have different childclasses of that, like OrderMoveForward or OrderJump.
    Now I have a list of orders somewhere that I want to save, and for that I have the following method in the Order class:
    public void saveToNBT(NBTTagCompound compound){
    compound.setByte("Id", (byte)Order.getIdFromOrder(this));//here is the problem

    compound.setFloat("Number", getNumber());
    compound.setString("String", getString());

    As you can see I want to save the ID of the Order that is calling this method and for that I need its instance or its class, but the problem is that "this" refers only to the superclass Order where the method is in.
    Is there a way to refer to the object that is actually calling the method or do I have to pass that to the method with an argument?(wich would be kinda silly I think)

    Sorry if that all sounds a bit incomprehensible.
  • 2013-08-25 19:33:55
    What does getIdFromOrder looks like? Is that one static? If it's not static there's no reason to send along this. If it's static, why? I don't see a reason for it being static.
  • 2013-08-26 05:41:09
    Right, now that you say that, it would probably be better if it wasnt. right now its basically a switch statement, but i guess a map that could be called from every object would be better. or is there something better than maps?
  • 2013-08-26 12:59:36
    An abstract method?
  • 2013-08-26 13:39:41
    Yea, but that would require me to write the id in every subclass and that would get confusing quickly to keep track of what ids i already used. I suppose i could make a generic class. to make my own map so it contains no duplicates or something.
  • 2013-08-26 17:06:04
    How would that be more confusing than making a generic class and your own map? You will still need to populate the map with all the subtypes.
  • 2013-08-26 19:01:03
    Ok sorry, nevermind that whole thread. I made a giant mess.
    I solved the problem by just having a static block at the start of Order:

    static List<Class> idList = new ArrayList<Class>();

    and then I have the method:

    public static int getIdFromOrder(Class order){
            for(int i = 0; i < idList.size(); i++){
                if(idList.get(i) == order){
                    return i;
            return -1;

    plus, for getting an instance of an Order:

    public static Order getOrderFromId(int id){
            try {
                return (Order) idList.get(id).newInstance();
            } catch (InstantiationException e) {
            } catch (IllegalAccessException e) {
            return null;

    No need for maps or generic classes here, I just kinda had a mental block for a while :S
  • Log in or sign up to reply to this thread