Zippy
Zippy

Reputation: 3887

Can a custom class know the name of the object that called it?

Is there anyway, when calling a method through an object (instance) for that method to know which instance (object) called it?

Here's an example (pseudo code) of what I mean:

Pseudo code example

public class CustomClass{


public void myMethod(){


    if (calling method is object1){

    //Do something here

    }

        else {

        //Do something else

        }


        }//End of method


}//End of class

And then in another class:

public SomeOtherClass{

CustomClass = object1;

public void someOtherMethod(){

object1 = new CustomClass();

object1.myMethod();    //This will call the 1st condition as the calling object is object1, if it were some other object name, it would call the 2nd condition.

    }//End of method

}//End of class

Possible work-around

The only way I've found to do this is to get the method to take another argument, say an 'int' and then check the value of that int and perform whichever part of the 'if else' statement relates to it (or 'switch' statement if definitely using an 'int' value) but that just seems a really messy way of doing it.

Upvotes: 0

Views: 390

Answers (3)

Mohayemin
Mohayemin

Reputation: 3870

What you need is the Strategy Pattern

public abstract class CustomClass {
    public abstract void MyMethod();
}

public class Impl1 extends CustomClass {
    @Override
    public void MyMethod() {
        // Do something
    }
}

public class Impl2 extends CustomClass {
    @Override
    public void MyMethod() {
        // Do something else
    }
}

Use it this way

public static void main(String[] args) {
    CustomClass myObject = new Impl1();
    // or CustomClass myObject = new Impl2();
}


As your comment says what you really need is perhaps the Template method Pattern

public abstract class CustomClass {
    public void myMethod(){ // this is the template method
        // The common things
        theDifferentThings();
    }

    public abstract void theDifferentThings();
}

public class Impl1 extends CustomClass {
    @Override
    public void theDifferentThings() {
        // Do something
    }
}

public class Impl2 extends CustomClass {

    @Override
    public void theDifferentThings() {
        // Do something else
    }
}

Upvotes: 4

Adam Siemion
Adam Siemion

Reputation: 16039

You can define a new attribute inside CustomClass which will store the identifier of the instance. If there will be only a few instances of CustomClass then you can use an enum type.

Replace:

object1 = new CustomClass();

with:

object1 = new CustomClass(1);

Add a new constructor and an attribute to CustomClass:

private int id;
public CustomClass(int id) {
    this.id = id;
}

Then you can replace:

if (calling method is object1){

with:

if (id == 1){

However, please keep in mind that this is a bad design. You should not have if conditions differing logic depending on the instance which called this method. You should should use polymorphism for such purpose.

Upvotes: 0

AlexR
AlexR

Reputation: 115378

You can know the name of current class by calling getClass().getName(). However you cannot know the name of object, moreover this does not have any meaning:

MyClass myObject1 = new MyClass();
MyClass myObject2 = myObject1;

myObject1.foo();
myObject2.foo();

Do you wutant foo() to know that it was invoked using myObject1 or myObject1? But both references refer to the same object!

OK, there are extremely complicated ways to know this. You can use byte code engineering using one of popular libraries like javassist, ASM, CGLib and inject missing information about the "object name" into byte code and then read this information. But IMHO this is not what you need.

Upvotes: 0

Related Questions