This question arose when creating multiple classes and having to create the same array several times in these classes:
private final static String[] types = { String.class.getSimpleName(), Integer.class.getSimpleName(),
Date.class.getSimpleName(), Boolean.class.getSimpleName() };
This Array contains the values: "String", "Int", "Date", "Boolean" , the purpose of the array is to contain the items of a JComboBox that will be used in these 2 classes:
Class 1:
public class Clase1 extends JPanel {
private final static String[] types = { String.class.getSimpleName(), Integer.class.getSimpleName(),
Date.class.getSimpleName(), Boolean.class.getSimpleName() };
JComboBox box = new JComboBox(types);
//codigo
}
Class2:
public class Clase2 extends JPanel {
private final static String[] types = { String.class.getSimpleName(), Integer.class.getSimpleName(),
Date.class.getSimpleName(), Boolean.class.getSimpleName() };
JComboBox box = new JComboBox(types);
//codigo
}
How can I avoid creating/recreating this object in multiple classes, are there any Best Practices for this?
Declare the static array in a class and then call it from whatever classes you need without changing it.
Then in your classes you access it with
Another way