Java design (using a finite field in functions)

I programmed Java for a year and took a break to make some python in the last year. I returned to java and I am confused by some design materials.

Say I have a class that performs almost all static

public class Example{

      String list = {{"A", "apple"}, {"B","banana"}, {"C", "can"}}

    public static manipulateTheList(){

        // do something with the above list

    }

    public static anotherManipulateTheList(){

        // do something with the above list

    }
}

And this will not allow me to use this.list, because the method is static. But I don’t think that I should define a list in a method every time, since all methods in the class will use the same list.

Is there a solution for this and what would be best practice for this kind of situation ..?

+4
source share
2 answers

static, , final:

private static final String LIST = {{"A", "apple"}, {"B", "banana"}, {"C", "can"}};

, :

Example.LIST

, ... , . static .

+4

, . ? , static , , ( public, private, protected, ). , .

final, , - , , - :

public static final String[][] LIST = {{"a","b"},{"c","d"}};
//can be named LIST_LETTERS too

, .

, , Util, String , parammeter, .

, .

+1

Source: https://habr.com/ru/post/1654084/


All Articles