Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Strings and instance variables  RSS feed

 
Otto Clanenburg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to access an instance variable of a class in a string?

Let's say that I have declared 2 instance variables of an object in my class:

private Object var1;
private Object var2;

What I want to do is to create a method that displays a dialogbox to confirm if these variables are doing a given task.
The method would be something like:

public void aMethod(Object aVar)

And the string within the method that I tried is:

"Do you want Object " + aVar + "to do the task?"

However, when I tried this, the string only displays the attributes of the object, instead of the different instance variables.

 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't tell what your goal is there, but in the String expression you're building, the toString() method of the object to which the variable aVar refers is going to be evaluated and the result inserted into the String.

You may be on the wrong track, or you may not, but it would be useful if you posted a less generic description of your requirement.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Otto Clanenburg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul, and hi everyone.

What I want to do is a loop that displays in turn different dialog boxes asking to perform a task for the different instance variables. These instance variables are instances of another object, however I have declared them in my class (caller). The question is if it is possible to display the different instance variables in a string.

It would be something like this:

"Do you want Object var1 to do the task?"

And next:

"Do you want Object var2 to do the task?"

Thank you
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're asking whether you can get the name of a Java variable which contains a reference to a particular object, then no you can't. But the user isn't going to know the names of variables in your code anyway so that wouldn't be a thing worth doing. These mysterious objects of yours should have an attribute which is visible to the user as their "name".
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand what you're trying to do correctly, you can't do it exactly that way.

1. You'd have to create a subclass of Object that has a name property
2. You'd have to override the toString() method for the name property to be displayed in a println call using only a reference to your object.


You can't do it with just Object because the declared type of a variable determines what you'll be able to ask of it. Declare an Object will therefore limit you to calling methods that are documented in the java.lang.Object API.

And just so you can make a distinction between the source code variable name and the object's name property, the code in main can be changed to:

The output of this version would be: Do you want the object Sam to do something?
(emphasis mine)
 
Otto Clanenburg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick reply Paul.

No, they don't have any name, and after reading your replies, I realized how silly was my question

 
Otto Clanenburg
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, thanks Junilu.

As Paul said I think it's not worth doing for my purpose, but thank you for your help.

I will try it just to learn.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!