0

I have classes similar to these:

class MyClass1 {

  static MyObject obj = new MyObject();

  public static void doSomething()
  { 
    MyClass1.obj.print();
    MyClass2.obj.print();
  } 

}    

class MyClass2 {

  static MyObject obj = new MyObject();

  public static void doSomething()
  {
    MyClass1.obj.print();
    MyClass2.obj.print();
  }

}

public class MyObject {

  public void print()
  {
    System.out.println("Which object called me?");
  }

}

From the print function in MyObject, how can I determine which object called the print function, the one in MyClass1, or the one in MyClass2?

Using a stack trace, I was only able to find the function that the print method is inside of and the class of the function that the print method is inside of.

Scott Seto
  • 15
  • 4
  • 2
    Are you designing something? Finding a bug? Learning? – Lukasz Madon Jul 21 '12 at 23:41
  • Doing so, doesn't seem to be useful. Why do you need to determine the `print()` method is being invoked by which object? – Lion Jul 21 '12 at 23:46
  • The MyObject class could contain important information about MyClass1 or MyClass2 and could contain a function that updates that information. Sometimes it is important to know whose information was just updated. – Scott Seto Jul 21 '12 at 23:50

3 Answers3

0

just

public void print()
{
    System.out.println("Which object called me?");
    if (MyClass1.obj == this) {
        System.out.println("MyClass1");
    } else if (MyClass2.obj == this) {
        System.out.println("MyClass2");
    }
}

...as long as both obj fields are initialized.

but, hmm, I wouldn't do this. Better tell MyObject this upon construction, give print a parameter or sub-class MyObject in MyClass1 and MyClass2 and override print.

Arne
  • 2,106
  • 12
  • 9
0

Why don't you make this is a first-class concept in your object model?

class MyClass1 {
    private static MyObject obj = new MyObject("MyClass1");
    public static void doSomething() {
        MyClass1.obj.print();
        MyClass2.obj.print();
    }
}

class MyClass2 {
    private static MyObject obj = new MyObject("MyClass2");
    public static void doSomething() {
        MyClass1.obj.print();
        MyClass2.obj.print();
    }
}

class MyObject {
    private final String name;
    MyObject(String name) {
        this.name = name;
    }

    public void print() {
        System.out.println("Q: Which object called me?");
        System.out.println("A: " + name);
    }
}
Binil Thomas
  • 13,699
  • 10
  • 57
  • 70
  • Instead of the String, you can also use the `Class` object (`MyClass1.class` or `MyClass2.class`) as the identifier. – Binil Thomas Jul 22 '12 at 05:49
-1

The following code should work:

public class MyObject {
    public static void main(String... args){
        MyClass2.doSomething();
    }


  public void print()
  {
    System.out.println(Thread.currentThread().getStackTrace()[2].getClassName() + " has me called.");
  }
}

See more at: http://download.oracle.com/javase/6/docs/api/java/lang/StackTraceElement.html or Get current stack trace in Java

Community
  • 1
  • 1
Matt3o12
  • 4,192
  • 6
  • 32
  • 47
  • 1
    this only prints the name of the class that contains the `doSomething` method - and that was already mentioned in the question. – Arne Jul 22 '12 at 00:02