In my source code for a mathematical project and I'm running into a problem that ends with a duplicate variable names error. In my function I take a string as an argument to be the name of a new array that will be allocated. Here's what the source code looks like Code: private static void somefunction(String name) { int name[] = new int[10]; ... } I need to call that function at least twice and I think that the compiler is getting confused and tries to allocate an array by the name of name instead of the string I provide as an argument. When it gets called the second time it tries to allocate name[] again and javac raises the error. Does anyone know how I could tell Java to name the array what I provide as an argument?
I'm afraid it is you rather than the compiler who is confused. Of course the array is called "name". That you passed a String variable also called "name" as an argument to the method is irrelevant. You could probably do what you want to do by using reflection, but I think it is highly unlikely that it would be worth the trouble. Of course, I have no idea what you are trying to achieve, but you might consider passing the array itself as an argument to the method, or using a Map<String, ArrayList> or something like that instead.