Home > database >  store object's name and reference it in the code
store object's name and reference it in the code

Time:11-13

I have two lists, after find which one has the highest value I store the other one's name into a string variable, like this:

iterateList = "lstDrives"; //lstDrives

So, I want to use that stored name in the next lines:

for(Integer currentElement : iterateList){
    if((mostExpensiveValue   currentElement) > b){
        result = -1;
    }
}

right now I obtained an error because I'm trying to iterate the String var and I want to refer to its content, my question is: how can I achieve this?

This is the entire piece of code:

if(mostExpensiveKb > mostExpensiveDv ){
    iterateList = "lstDrives"; //lstDrives
    mostExpensiveValue = lstKb.get(keyboards.length-1);
} else{
    iterateList = "lstKb"; //listKb
    mostExpensiveValue = lstDrives.get(keyboards.length-1);
}


for(Integer currentElement : iterateList){
    if((mostExpensiveValue   currentElement) > b){
        result = -1;
    }

}

CodePudding user response:

That's not how java works. Object do not have names; variable names do not survive the compilation process.

Object o = new Object();

This does not mean that your object's name is 'o'. After all:

Object o = new Object();
Object v = o;

There is only one object here (to make an object, new must be invoked; it is only invoked once here, therefore, there cannot possibly be 2 objects). You have 2 variables that are both referring to the same object. It's like having 2 post-it notes with the same house address written on each. That doesn't mean you have 2 houses.

So, does that mean this one object's name is both o and v?

Object o = new Object();
Object v = o;
o = null;
v = null;

Does that mean that the name of the object used to be o, but then it became both o and v, and then it became just v, and then it became nameless?

Hopefully this gives you the insight as to why objects do not have names and java is not now and not ever going to let you do something like obj.getName().

Java compiles code to an intermediate step (.class files) before you run them. In this intermediate step, local variable names disappear; they can remain for debug purposes but the actual act of running class files ignores this, and you can see this in class files: The local var names don't need to be there in the first place (depends on whether you're using -g when compiling or not to save debug vars), and if they are there, it's in a separate attachment to a method which the JVM just skips completely when reading a class file.

As a consequence, something like getVar("varName") does not now and will never exist in java either. Let alone that java is strongly typed and getVar wouldn't let you type it, given that "varName" could as far as you know, be supplied by the user on the fly.

In other words, you're not 'thinking in java' here. What you're trying to do doesn't work, never has, and never will.

What you can instead do is simply make another variable. After all, all non-primitive variables are merely references (postit notes that contain an address to an object, NOT the object itself). Postit notes are cheap. Here:

List<String> someList = readTheCollectedWorksOfShakespeare();

Let's assume here that this method makes a humongous list with all the lines of everything shakespeare ever wrote. That's a sizable list! Let's say 100MB's worth, so your VM's memory load spikes considerably after this line. Then you do:

List<String> list2 = someList;
List<String> list3 = someList;
List<String> list4 = someList;
List<String> list5 = someList;

and check memory load again: No effect. These are 4 references (pointers). The above 4 lines execute pretty much instantaneously, and cost no memory. It's just that you now have 5 post it notes, all with the address to some ginormous castle on it. The computer did not have to painstakingly copy the castle brick by brick. You can do the same thing in your code:

List<Integer> iterateList;
if (mostExpensiveKb > mostExpensiveDv) {
    iterateList = lstDrives;
    mostExpensiveValue = lstKb.get(keyboards.length-1);
} else {
    iterateList = lstKb;
    mostExpensiveValue = lstDrives.get(keyboards.length-1);
}


for (Integer currentElement : iterateList) {
    if ((mostExpensiveValue   currentElement) > b) {
        result = -1;
    }
}

CodePudding user response:

You can use an hashMap to store the list name like key and the list object like value:

Map<String, List<Integer>> namesAndLists= new HashMap<String, List<Integer>>();
namesAndLists.put("lstDrives",lstDrives);
namesAndLists.put("lstKb",lstKb);
  • Related