0

I wanna print the name of an array within a function that I pass as an String argument:

public static String[][] cleanCSVtable(String[][] data_table) {}

instead of something like this:

[[Ljava.lang.String;@ba4539 

For example lets assume that I have defined this array outside of the main function as

public static String [][] XYZ = null; 

I wanna print XYZ within this function instead of

[[Ljava.lang.String;@ba4539. 

Any hacks/hints/ideas?

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
user706838
  • 5,132
  • 14
  • 54
  • 78
  • The question is difficult to read. In a nutshell, no, you can't do this in any reasonable way. You could examine the bytecode and possibly get at the info, but ew. – Dave Newton Mar 09 '12 at 18:05
  • 2
    The name of the array, so far as there is one, is `[[Ljava.lang.String;@ba4539`. `XYZ` is just the name of one reference of many to it, `data_table` is another. – ibid Mar 09 '12 at 18:06
  • 1
    see answer to this question [Java Reflection: How to get the name of a variable?](http://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable) – Alex Mar 09 '12 at 18:09
  • Good luck! Aside from using debugging interfaces there's no way. And even the debugging interfaces won't help much if you want the name used in the caller, unless you do a lot of analysis of the call stack, etc. – Hot Licks Mar 09 '12 at 18:27

3 Answers3

4

That is not possible. XYZ is only the variable name and can't be accessed. If you're really dependent on some textual description of what the array is about, you could wrap it in a class which contains a String or enum field called description or similar.

wonderb0lt
  • 2,035
  • 1
  • 23
  • 37
2

Objects don't have names unless you're talking about a specific type which has a name property.

In this case, XYZ is the name of a variable, not an object. Two (or more) variables may refer to the same object. For example:

String[][] foo = new String[10][50];
String[][] bar = foo;

It sounds like perhaps you need a CsvTable class which contains the string array and has a separate variable for the name.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Jon and Sorvahr are right. There are no conventional ways to find the variable name that holds the array. Moreover in your case it is not primitive, so the variable contains only reference to the array. There can be a lot of references that point to the same array, so which one do you want to discover?

But there are other ways that sometimes can help you. Java byte code contains names of class fields, so if your variable is define on class level and not the internal variable of method you can parse bytecode yourself and extract this name.

Obviously it is not so simple task but there are tools that can help you. During the last week I played a little bit with Javassist that definitely allows you to do this. You can discover byte code of running class and extract the name of field that contains reference to specific array.

The only question I still have is "why"?

AlexR
  • 114,158
  • 16
  • 130
  • 208