0

So I'm learning java and I don't know how to use a string as an object name.

For example, let's suppose I have 3 objects not belonging to the same class, but they all have the variables a, b, c. I want a function to modify these variables, depending on the object name I give, which would work like :

public void myFonction(String objectName)
{
   objectName.a++; //of course that doesn't word
   objectName.b=15;
//ect
}

This must be something extremely simple to do, but what I found on the subject didn't fit this case or I didn't understand :)

6
  • That's not how you do things in Java. A lot of concepts involved here. Java is pass by value. Java does not have dynamic variables names. You might be looking for Reflection. Commented Apr 13, 2014 at 16:02
  • 3
    Generally speaking, this isn't something you should want to do; object names are a compile-time thing, so why are you in a position that you only know the variable name at runtime in a String? In some cases, reflection is the answer (although it very rarely is); an alternative is to create a Map that maps String -> Object. Commented Apr 13, 2014 at 16:02
  • Also, an object does not have a name. A variable has a name. Commented Apr 13, 2014 at 16:02
  • Why not just use instanceof? Commented Apr 13, 2014 at 16:04
  • Why do you need an object by string? Knowing will help us to propose a good solution for you. Commented Apr 13, 2014 at 16:19

3 Answers 3

1

What you want is not done like that in Java.

It would be done like this:

public class Parent {

   protected int a;
   protected int b;
   protected int c;
}

Then you would have

public class A extends Parent {
   //add whatever else you need
}

public class B extends Parent {
   //add whatever else you need
}

public class C extends Parent {
   //add whatever else you need
}

and finally your method would be:

public void myFonction(Parent object)
{
   object.a++;
   object.b=15;

}
Sign up to request clarification or add additional context in comments.

3 Comments

Huh? What is the relevance of the three child classes?
I think you wanted to say object in the method.
The description of the problem said that there are objects of different type but with common fields a,b and c
0

If they all have the variables a, b and c why not make the classes extend a master class which uses these variables. Then you can call the function via that master class.

E.g.

Entity Class has variables a, b, c

public void myFunction(Entity entityName){
    entityName.a++;
    entityName.b=15;
}

Class Dog extends Entity and so calling myFunction with an instance of Dog would allow you to edit these values as would calling an Instance of the Class Cat which also extends Entity.

2 Comments

What's the difference between your answer and geoand's?
@Doon His answer wasn't there when i was typing it out.
0

You could do this using a map which stores objects by name.

Map<String, YourObject> map = new HashMap<>();
map.put ("Name1", new YourObject("Name1"));

Then you can lookup the object by name and manipulate

String nameInput = "Name1";

YourObject obj = map.get(nameInput);
obj.incrementCounter(); // assuming you have this method

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.