Reputation: 121
String[] names = {"abc"};
for this array can we retrieve the array's name names
?
We can get the class of the array, but can we get the name of the array?
Upvotes: 2
Views: 5784
Reputation: 1357
You could achieve similar functionality by creating your own class with a attribute called "name". The simplest example:
MyArray myArray = new MyArray();
class MyArray{
public String name = "myArray";
public String[] names = {"Name1", "Name2", "Name3"};
}
If you wanted to have a different name for each instance make a constructor that accepts a name as an input parameter:
MyArray myArray = new MyArray("myCustomName");
class MyArray{
public String name = "myArray";
public String[] names = {"Name1", "Name2", "Name3"};
public MyArray(String name){
this.name = name;
}
}
Upvotes: 0
Reputation: 4713
As a general rule no, it's not possible. However, there are some circumstances when you can obtain the name of a variable through reflection:
Upvotes: 4