What level of encapsulation do the attributes of the classes in which the logic of an activity in Android have been programmed?


I'm new to programming for Android in Java, I've been watching one or two tutorials to learn. I know that each activity is composed of an .xml file that describes the components that the activity will have and a class in Java where the logic of each of these components is programmed.

I have the following code:

public class MainActivity extends AppCompatActivity {

Button btn_buscarSala;

protected void onCreate(Bundle savedInstanceState) {...}

As far as I understand, the components that are going to be used in the activity are declared in the class before instantiating them in the possible methods that the class has. Like any normal attribute of a class in java.

My question is, these "attributes" (like the Button object in the example) what level of encapsulation do they have? and is there a good practice convention that says how to define it?

should I declare them as private or would that cause some kind of error?

asked by S.Pardo 16.11.2018 в 18:28

1 answer


The activities programmed in Java must follow the directives of good practices of this language, these same say that no element can have a level of encapsulation public , its level of encapsulation must be > protected or private .

On the other hand, the components must not be accessible directly, that is, there must not be a getButtonFoo (), these components can interact with the external environment indirectly, that is, through services or listeners.

answered by 17.11.2018 / 06:00