1

For e.g., is the following code is right?

private LinearLayout layout1;
private LinearLayout layout2;
private LinearLayout layout3;
Ganesh Kanna
  • 2,269
  • 1
  • 19
  • 29
  • 1
    Could you please elaborate? What kind of harm do you mean? Normally using `private` modifier for the fields inside a class is recommended to ensure proper encapsulation, but of course that depends on your specific use case. – Egor Nov 14 '16 at 10:51
  • There is no harm if we use private access modifier. Yes your code is right as long as you do not want to access those view outside your class. – Shadow Droid Nov 14 '16 at 10:53

1 Answers1

6

No, there is no harm in it. However, if you are using some kind of view injection library like ButterKnife, it won't let you define your views as private. The reason is the following:

The reason that Butter Knife requires views not be private is that it actually generates code which sets the fields. The code that it generates lives in the same package as your class which is why the field must be package-private, protected, or public. If the field was private the generated code would fail to compile since it cannot access the private field.

Source: Butterknife View injection

Community
  • 1
  • 1
Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73