Yes it can be done from a conceptual point of view.
You want to be able to associate values ("aa", "bb" ..) with some "variables" to which you give some names ("a1", "a2" ...). And you want to be able to access those "variables" by building your name dynamically.
I put variables in quotes because in the strict case of variables within a method, which are those that appear in your example, it is not possible, that I know, not even with reflection. But it is possible to have a mechanism that allows you to associate values with names. That mechanism is called MAP .
A map is an associative container. It comes to be like an array. But in an arrangement the access index can only be an integer, in a map it can be any type of data, and in the maps it is usually called key instead of index.
With the following code, we create a map that associates values of type ValidValue with keys of type TipoClave.
Map<TipoClave, TipoValor> var = new HashMap<>();
In our case we will use String as KeyType and String as ValidType.
To assign (or using the terminology of the maps associate ) a value to a key we use put. The first four lines of the original author's main would look like this:
Map<String, String> variables = new HashMap<>();
variables.put("a1", "aa");
variables.put("a2", "bb");
variables.put("a3", "cc");
variables.put("a4", "dd");
To read the value associated with a key we use the get method. The original author's code using maps would be:
for(int i = 1; i<=4; i++){
String a5 = ("a"+ i);
System.out.println("a"+i +"= "+ variables.get(a5) );
}