Results 1 to 3 of 3

Thread: caller info

  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default caller info

    Class A creates an object of Class B and uses that object to instantiate a method C. How can i tell inside method C that 'this' was called by A?

    Inside class A:

    B bbb = new B();
    bbb.doSomethingClever();

    Inside class B:
    inside doSometihngClever () {
    I got called from class A ... <-- how can i get this information?
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mac View Post
    Class A creates an object of Class B and uses that object to instantiate a method C. How can i tell inside method C that 'this' was called by A?

    Inside class A:

    B bbb = new B();
    bbb.doSomethingClever();

    Inside class B:
    inside doSometihngClever () {
    I got called from class A ... <-- how can i get this information?
    }
    Maybe the following can be of any help:

    Java Code:
    public class AAA {
    	
    	public static void main(String[] args) {
    
    		new BBB().method();
    	}
    }
    
    class BBB {
    	
    	public void method() {
    		Exception ex= new Exception();
    		ex.fillInStackTrace();
    		StackTraceElement[] ste= ex.getStackTrace();
    		
    		for (int i= 0; i < ste.length; i++)
    			System.out.println(ste[i].getMethodName()+" in "+ste[i].getClassName()+" at line "+ste[i].getLineNumber());
    	}
    }
    Note that the JVM is free to skip StackTraceElements (thanks to call optimizations).

    kind regards,

    Jos

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

Similar Threads

  1. Best way to do a pop-up 'info bubble'
    By lightstream in forum AWT / Swing
    Replies: 2
    Last Post: 01-27-2009, 07:39 AM
  2. INFO About JDK1.6
    By ravi in forum New To Java
    Replies: 2
    Last Post: 11-03-2008, 02:30 PM
  3. Looking for some info.
    By Ragster in forum JDBC
    Replies: 2
    Last Post: 01-17-2008, 05:19 PM
  4. Getting Header Info
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-26-2007, 01:30 PM
  5. JVM Info
    By kishi in forum Advanced Java
    Replies: 0
    Last Post: 11-16-2007, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •