473,327 Members | 2,094 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,327 software developers and data experts.

returning declared object name

Hi,

when i create an object, is there a way to return the name of that declared object? for example, if i create

ClassObject demo = new ClassObject();

how can i return the name of my object, namely "demo" as a string?

i've tried playing around with the fact that i can obtain the class name from my object, but i can not seem to find any methods that i need... can anyone guide me please?

thanks
joe
Dec 7 '09 #1

✓ answered by chaarmann

Read about Introspection, you could wrap all your classes in another class, let's say AllObjects, and investigate that class.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Class AllObjects
  3. {
  4.  ClassObject demo = new ClassObject();
  5.  ClassObject demo2 = new ClassObject();
  6.  ...
  7. }
  8.  
  9. ...
  10. foo = new AllObjects();
  11. System.out.println(foo.getClass().getFields());
  12. ...
  13.  


But probably what you want is much easier to achieve:
Just pass a name for the constructor and write a getName() method!
Expand|Select|Wrap|Line Numbers
  1.  
  2. ClassObject demo = new ClassObject("myName");
  3. ...
  4. String name= demo.getName();
  5. ...
  6.  
  7. Class ClassObject()
  8. {
  9.   public String name;
  10.  public void ClassObject(String name)
  11.  {
  12.    this.name=name;
  13.  }
  14.  public string getName()
  15.  {
  16.    return name;
  17.  }
  18. }
  19.  

2 2936
chaarmann
785 Expert 512MB
Read about Introspection, you could wrap all your classes in another class, let's say AllObjects, and investigate that class.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Class AllObjects
  3. {
  4.  ClassObject demo = new ClassObject();
  5.  ClassObject demo2 = new ClassObject();
  6.  ...
  7. }
  8.  
  9. ...
  10. foo = new AllObjects();
  11. System.out.println(foo.getClass().getFields());
  12. ...
  13.  


But probably what you want is much easier to achieve:
Just pass a name for the constructor and write a getName() method!
Expand|Select|Wrap|Line Numbers
  1.  
  2. ClassObject demo = new ClassObject("myName");
  3. ...
  4. String name= demo.getName();
  5. ...
  6.  
  7. Class ClassObject()
  8. {
  9.   public String name;
  10.  public void ClassObject(String name)
  11.  {
  12.    this.name=name;
  13.  }
  14.  public string getName()
  15.  {
  16.    return name;
  17.  }
  18. }
  19.  
Dec 7 '09 #2
thank you so much! extremely helpful!
Dec 7 '09 #3

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

Similar topics

7
by: Dr John Stockton | last post by:
What are the best ways of returning multiple results from a subroutine ? I've been using ... return } which is inelegant. I'm used to Pascal's procedure X(const A, B : integer; var C, D :...
4
by: mchoya | last post by:
I'm so frustrated. I'm beginning school next week and I have been working on a simple program for several days now without being able to complete it. (Though I have brushed up on a lot of C++...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
8
by: briforge | last post by:
I am writing a program for the Palm OS in c++. I have already posted this to a palm development forum but I'm desperate for a fix, so I hope this cross-post isn't offensive. My program is...
1
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
1
by: Eniac | last post by:
Hi there, is there any way I can make a property returning another type than what is declared in the "as" statement ? ex.: public property Age as Integer Get return m_intAge
47
by: pkirk25 | last post by:
I've made a small program to demonstrate one problem I'm having fixing strings in C. I need to be able to remove HTML mark-ups from text lines. I create my variable, pass it to my function,...
6
by: student1976 | last post by:
All Beginner/Intermediate level question. I understand that returning ptr to local stack vars is bad. Is returning foo_p_B from fnB() reliable all the time, so that using foo_p_A does not...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
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.