Get class name of instantiating class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nmb3000
    New Member
    • Mar 2007
    • 3

    Get class name of instantiating class

    Java 1.5

    I'm not sure if this is possible, but I'd like to get the class name of the class that instantiated my class. For example:
    Code:
    public class Foo {
      public static void main(String args[]) {
        Bar b = new Bar();
      }
    }
    
    ...
    
    public class Bar {
      public Bar() {
        System.out.println(parent.class.getName());
      }
    }
    Would output the name of Foo's class. Is that possible?

    Thanks!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by nmb3000
    Java 1.5

    I'm not sure if this is possible, but I'd like to get the class name of the class that instantiated my class. For example:
    Code:
    public class Foo {
    public static void main(String args[]) {
    Bar b = new Bar();
    }
    }
     
    ...
     
    public class Bar {
    public Bar() {
    System.out.println(parent.class.getName());
    }
    }
    Would output the name of Foo's class. Is that possible?

    Thanks!
    If Foo f = new Foo();

    then
    Code:
    f.getClass()
    prints class Foo

    Comment

    • nmb3000
      New Member
      • Mar 2007
      • 3

      #3
      Sorry, perhaps I wasn't very clear.

      Foo in this case is a class I have no control over. I am writing Bar, and it will be instantiated as an object in Foo. I would like to know if there is a way that, from within Bar, I can find out what Foo's class name is.

      Thanks again.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by nmb3000
        Sorry, perhaps I wasn't very clear.

        Foo in this case is a class I have no control over. I am writing Bar, and it will be instantiated as an object in Foo. I would like to know if there is a way that, from within Bar, I can find out what Foo's class name is.

        Thanks again.
        You can't. Why do you want to do it anyway?

        You cannot write a class and restrict where that class will be instantiated. If you write Bar, fine. Whether Bar will be instantiated in Foo or somewhere else cannot be hardcoded into the class Bar.

        Comment

        • nmb3000
          New Member
          • Mar 2007
          • 3

          #5
          Originally posted by r035198x
          You can't. Why do you want to do it anyway?

          You cannot write a class and restrict where that class will be instantiated. If you write Bar, fine. Whether Bar will be instantiated in Foo or somewhere else cannot be hardcoded into the class Bar.
          Hmm, it seems we're just not connecting here.

          I don't care what classes instantiate my class. What I want to do is from within *my* class, get the name of whatever class it was instantiated *from*.

          So say I wrote Bar. You have a program named Bob. You instantiate Bar from within Bob. I want Bar to discover that your class is named Bob.

          Is this possible? Thanks again.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by nmb3000
            Hmm, it seems we're just not connecting here.
            Pity

            Originally posted by nmb3000
            I don't care what classes instantiate my class. What I want to do is from within *my* class, get the name of whatever class it was instantiated *from*.
            Classes do not instantiate other classes

            Originally posted by nmb3000
            I want Bar to discover that your class is named Bob.
            Bar can only do that inside Bob by making notorious use of an interface. You force all classes that are going to instantiate your class(Bar) to implement the InstanitiatesBa r interface

            Kids, do not try this at home.


            Code:
             interface InstantiatesBar { 
            }
            class Bar {
             InstantiatesBar object;
               Bar(InstantiatesBar object) { //constructor requires an InstantiatesBar object
              this.object = object;
            
               }
            }
            class Bob implements InstantiatesBar {
            	   Bob() {
            		  Bar bar = new Bar(this);
            		  System.out.println(bar.object.getClass());
            	   }
            	   public static void main(String ... args) {
            	 new Bob();
            	}
            }
            Running that should print class Bob

            There is a huge flaw in that code
            Say some one writes another class

            Code:
             class Lier implements InstantiatesBar { 
            	   Lier() {
            		  Bob bob = new Bob();
            		  Bar bar = new Bar(bob);
            		  System.out.println(bar.object.getClass());
            	   }
            	   public static void main(String ... args) {
            	 new Lier();
            	}
            and you can guess the output to that.

            Comment

            Working...