aspose file tools*
The moose likes Beginning Java and the fly likes Is there any way an initialized instance knows who initialized it without passing information to it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there any way an initialized instance knows who initialized it without passing information to it?" Watch "Is there any way an initialized instance knows who initialized it without passing information to it?" New topic
Author

Is there any way an initialized instance knows who initialized it without passing information to it?

Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 125
Hi,

Consider a class Test. In the constructor of Test (or a method of Test) a class Foo is declared and initialized.
Question: is it possible that Foo knows who initialized it? That is, without sending an instance of Test to the constructor of Foo.

Thanks!
Kjeld



Kjeld Sigtermans - SCJP 1.4 - SCWCD 1.4
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

I don't think so.
Usually, in such scenarios, you pass a reference of Test to Foo.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18973
    
  40


Interestingly, this *is* possible. It is just not guaranteed to work.

In the Foo constructor, create an Exception --> get the stack trace elements. For each stack trace element, get the class name --> get the class object via class.forName(). The first stack trace element class that is not the Foo class object, should be the class that initialized.

Of course, on some JVMs, it is possible to turn off stack tracing, so this may not work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

Henry!
You think of everything! This is a very interesting usage of Exceptions. I must remember this.
Thank you.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

That's a clever trick, but I would never use something like that in a real application.

Kjeld, why does your class Foo need to know this? When you encounter unusual requirements like this while writing a program, it's usually a sign that there's a problem with the design of your program. Can't you solve whatever the problem is with OO techniques? (Polymorphism, etc.).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Kjeld Sigtermans
Ranch Hand

Joined: Aug 10, 2006
Posts: 125
Well it's just something I wanted to know. We're using Log4J logging in our project and for several reasons we'd like to create our own Logger class, encapsulating the Log4J functionality. What I'd like to do, is to instantiate our own Logger class without having to tell it who instantiated it. But perhaps I could pass it a 'this' reference and use the getClass() method.
Love the Exception solution LOL but of course that's a very very 'dirty' solution.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there any way an initialized instance knows who initialized it without passing information to it?