-2

Should I declare an Android class member protected? What would be the benefits? All examples I see just declare the field type and its name ("package protected").

protected TextView mName;

vs

TextView mName;

I don't need to extend such class.

ticofab
  • 7,551
  • 13
  • 49
  • 90
  • do you extend the class where you declared `mName` and access if from on of the child class? If the answer is no, than you don't need protected. The second allows you to access it, trough an object of the class where it is defined, on a package level. Do you want that? If the answer is no, then probably you want mark it as `private` – Blackbelt Apr 09 '15 at 08:02
  • thanks for pointing this out @Blackbelt. No I don't, and I edited the question accordingly. – ticofab Apr 09 '15 at 08:04
  • then what are your doubts ? – Blackbelt Apr 09 '15 at 08:08
  • I'm just curious to see if people have something to point out that I don't know yet :) – ticofab Apr 09 '15 at 08:12
  • No kidding, guy, of course you know access permissions of fields of a class. – SilentKnight Apr 09 '15 at 08:38

2 Answers2

0

Protected: variable is accessible for that class and other classes which extended that class

Private: variable is accessible for that class only

Public: variable is accessible for that class and other classes even if they do not extend this class. You need to create instance of class in order to access its public variables.

Public static: variable is accessible for that class and other classes even if they do not extend this class. You DO NOT need to create instance of class in order to access its public static variables.

Public static final: variable is accessible for that class and other classes even if they do not extend this class. You DO NOT need to create instance of class in order to access its public static variables.Also you CAN NOT change variable's value since it is final.

Jemshit
  • 9,501
  • 5
  • 69
  • 106
0

I would just use ButterKnife library as per http://jakewharton.github.io/butterknife/ which needs those field variables to be public anyways.

//Activity
@InjectView(R.id.your_textview_name)
public TextView mText;

@Override
public void onCreate(Bundle saveinstanceState) {
    super.onCreate(saveinstanceState);
    setContentView(R.layout.your_layout);
    ButterKnife.inject(this);
}

//Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveinstanceState) {
    View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
    ButterKnife.inject(this, view);
    return view;
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • it does not always needs to be public, if you dont want that variable to be accessable from other classes, private is preffered, which prevents accident changes from other classes. That is meaning of encapsulation... – Jemshit Apr 09 '15 at 08:58
  • 1
    I wonder what does a third-party library has to do here? What OP asked is simple and straightforward. Your answer seems very irrelevant. – waqaslam Apr 09 '15 at 09:10