• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think so.
Usually, in such scenarios, you pass a reference of Test to Foo.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry!
You think of everything! This is a very interesting usage of Exceptions. I must remember this.
Thank you.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.).
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic