Making the map available
First, it is necessary to define some way to retrieve the map that is in the Classe1
. There are several ways to do this...
1. Instance attribute
The map could be put into an instance attribute, with the values initialized in the constructor:
public class Classe1 {
public Map<String, String> teste = new Hashmap<>();
public Classe1() {
teste.put("TESTE", "exemplo");
}
}
2. Method that returns the instance map
However, it is not recommended to have public attributes. This breaks the class encapsulation. So we could have a method getter:
public class Classe1 {
private Map<String, String> teste = new Hashmap<>();
public Classe1() {
teste.put("TESTE", "exemplo");
}
public Map<String, String> getMapa() {
return teste;
}
}
3. Method that returns a new map
If there is need to create several different maps depending on some situation, create the map in a method and return it:
public class Classe1 {
public Map<String, String> getNovoMapa()
Map<String, String> teste = new Hashmap<>();
teste.put("TESTE", "exemplo");
return teste;
}
}
Making the map available statically
If the content of the map is not dependent on an instance of Classe1
, for example if the values are fixed, then it need not depend on an instance of the Classe1
.
There are also several ways to do this:
1. Static attribute
The map could be put into a static attribute, with the values initialized into a static boot block:
public class Classe1 {
public static Map<String, String> testeEstatico = new Hashmap<>();
static {
testeEstatico.put("TESTE", "exemplo");
}
}
2. Static method returning the class map
However, we have in the previous example the problem with class encapsulation. So we could have a method getter static:
public class Classe1 {
private Map<String, String> testeEstatico = new Hashmap<>();
static {
testeEstatico.put("TESTE", "exemplo");
}
public static Map<String, String> getMapaEstatico() {
return testeEstatico;
}
}
3. Static method that returns a new map
To return a new map statically:
public class Classe1 {
public static Map<String, String> getNovoMapaEstatico()
Map<String, String> testeEstatico = new Hashmap<>();
testeEstatico.put("TESTE", "exemplo");
return testeEstatico;
}
}
Accessing the map
If the map is not static, the Classe2
must have a reference to an instance of Classe1
.
This can be done in many ways...
1. Instantiating the Classe1
directly
public class Classe2 {
public void metodo() {
Classe1 classe1 = new Classe1();
//recupera novo mapa
Map<String, String> testeNovo = classe1.getNovoMapa();
//recupera mapa da instância através do getter
Map<String, String> teste = classe1.getMapa();
//recupera mapa diretamente do atributo da instância (não recomendado)
Map<String, String> teste2 = classe1.teste;
}
}
2. Receiving an instance of Classe1
per parameter in the method
public class Classe2 {
public void metodo(Classe1 classe1) {
//recupera novo mapa
Map<String, String> testeNovo = classe1.getNovoMapa();
//recupera mapa da instância através do getter
Map<String, String> teste = classe1.getMapa();
//recupera mapa diretamente do atributo da instância (não recomendado)
Map<String, String> teste2 = classe1.teste;
}
}
3. Having an authority of Classe1
within the Classe2
In this case, the attribute classe1
must be initialized in some way.
public class Classe2 {
Classe1 classe1;
//inicialização via construtor
public Classe2(Classe1 classe1) {
this.classe1 = classe1;
}
//inicialização via setter
public void setClasse1(Classe1 classe1) {
this.classe1 = classe1;
}
public void metodo() {
//recupera novo mapa
Map<String, String> testeNovo = classe1.getNovoMapa();
//recupera mapa da instância através do getter
Map<String, String> teste = classe1.getMapa();
//recupera mapa diretamente do atributo da instância (não recomendado)
Map<String, String> teste2 = classe1.teste;
}
}
Accessing the map statically
If the map is static, you can access it directly from any point in the code.
public class Classe2 {
public void metodo() {
//recupera novo mapa
Map<String, String> testeNovo = Classe1.getNovoMapaEstatico();
//recupera mapa da classe através do getter
Map<String, String> teste = Classe1.getMapaEstatico();
//recupera mapa diretamente do atributo da classe (não recomendado)
Map<String, String> teste2 = Classe1.testeEstatico;
}
}
Considerations
There are advantages and disadvantages in the various approaches. In short:
- Avoid accessing attributes directly
- For "constant" maps (which do not change), use static methods that return a static attribute
- When there is a dependency relationship between two classes, avoid using
new
directly, always prefer to receive the instance via constructor or Setter. This is the concept of Control Inversion, which decreases the coupling between classes, facilitates the creation of unit tests and helps in understanding the code, because it leaves the explicit dependency.
Hi. I don’t see the contents of Classe2?
– Cold
See: http://answall.com/help/mcve. Help us help you.
– Maniero
I didn’t put anything in the content of Classe2 because I just wanted to know what I have there, so I could use the values of the Hashmap of Classe1 ! I have to create a method in Classe1 that returns the Hashmap and then add it to a new Hashmap created in Classe2 ?
– HugoMachado