You cannot do this with fields since you cannot declare them twice. However, you can create a local vairable in a method with the same name.
Fields
This will NOT WORK EVER because you create a variable called list1
and you cannot create a second variable with the same name. No matter what the type is!
public class SomeClass {
String list1;
ArrayList<String> list1 = new ArrayList<String>();
}
Local variables
However, you can create a local variable with the same name inside a method. This variable will overlay the original (the original will not be "deleted" or so, you can still access it with this.list1
). Note that this is considered a bad pratice, since it makes you code very hard to read.
public class SomeClass {
String list1;
public void myMethod(){
ArrayList<String> list1 = new ArrayList<String>();
// to get the original string do:
this.list1;
}
}