473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. public class Foo {
  2.   public static void main(String args[]) {
  3.     Bar b = new Bar();
  4.   }
  5. }
  6.  
  7. ...
  8.  
  9. public class Bar {
  10.   public Bar() {
  11.     System.out.println(parent.class.getName());
  12.   }
  13. }
  14.  
Would output the name of Foo's class. Is that possible?

Thanks!
Mar 12 '07 #1
5 4428
r035198x
13,262 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. public class Foo {
  2. public static void main(String args[]) {
  3. Bar b = new Bar();
  4. }
  5. }
  6.  
  7. ...
  8.  
  9. public class Bar {
  10. public Bar() {
  11. System.out.println(parent.class.getName());
  12. }
  13. }
  14.  
Would output the name of Foo's class. Is that possible?

Thanks!
If Foo f = new Foo();

then
Expand|Select|Wrap|Line Numbers
  1. f.getClass()
prints class Foo
Mar 12 '07 #2
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.
Mar 12 '07 #3
r035198x
13,262 8TB
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.
Mar 12 '07 #4
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.
Mar 15 '07 #5
r035198x
13,262 8TB
Hmm, it seems we're just not connecting here.
Pity

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

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 InstanitiatesBar interface

Kids, do not try this at home.


Expand|Select|Wrap|Line Numbers
  1.  interface InstantiatesBar { 
  2. }
  3. class Bar {
  4.  InstantiatesBar object;
  5.    Bar(InstantiatesBar object) { //constructor requires an InstantiatesBar object
  6.   this.object = object;
  7.  
  8.    }
  9. }
  10. class Bob implements InstantiatesBar {
  11.        Bob() {
  12.           Bar bar = new Bar(this);
  13.           System.out.println(bar.object.getClass());
  14.        }
  15.        public static void main(String ... args) {
  16.      new Bob();
  17.     }
  18.  
Running that should print class Bob

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

Expand|Select|Wrap|Line Numbers
  1.  class Lier implements InstantiatesBar { 
  2.        Lier() {
  3.           Bob bob = new Bob();
  4.           Bar bar = new Bar(bob);
  5.           System.out.println(bar.object.getClass());
  6.        }
  7.        public static void main(String ... args) {
  8.      new Lier();
  9.     }
  10.  
and you can guess the output to that.
Mar 15 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Alex Hunsley | last post by:
Does python provide a way to dynamically use modules and/or classes? I'm thinking in the vein of Java's Class.forName. As a pseudocode example, I'm looking for the following ability: ...
3
by: Nagesh | last post by:
hi, I have seen the winvnc(tightvnc server) source code in this I seen that class member funtions are calling without instantiating the object i.e. like vncService::ShowDefaultProperties() where...
5
by: AlexVN | last post by:
Hi, I would like to know if someone investigated the method of creating classes dynamically depending on the name. I do not like a lot of ifs and I suppose that something like...
10
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.