Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

public or private, does it really matter with Android variables

inside of a single activity, when defining components to be used only within that activity, what's the real difference between the following definitions:

Button  btnPower = null; //or private Button btnPower = null; //or public Button btnPower = null;  public void somethingUsingTheButton(){   btnPower = (Button)findViewById(R.id.btnpower_id); } 

are there some "under the hood" conventions that should be thought about (garbage cleanup, memory, etc) that would suggest to always use private over public, if the entity itself is only ever going to be used inside the class it's written in?

like image 479
Octoth0rpe Avatar asked Sep 19 '12 18:09

Octoth0rpe


People also ask

Should variables be private or public?

It is considered better programming practice to use private rather than public class variables, and you should aim to do this in the remainder of the course. Here is the Complex class rewritten using only private class variables.

What is the point of making variables private?

Making a variable private "protects" its value when the code runs. At this level, we are not concerned with protecting it from other programmers changing the code itself. The point of so-called "data hiding" is to keep internal data hidden from other classes which use the class.

What is the difference between public and private variables?

Public variables, are variables that are visible to all classes. Private variables, are variables that are visible only to the class to which they belong. Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.

What is public and private in Android?

private – accessible within class only. default – accessible available to the package only. protected – accessible within package and outside the package but through inheritance only. public – accessible everywhere.


2 Answers

Private fields promote encapsulation

It's a generally accepted convention to use private unless you need to expose a field or method to other classes. Getting in this as a habit will save you a lot of pain in the long run.

However, there isn't anything inherently wrong with a public field or method. It causes no difference for garbage collection.

In some cases some types of access will affect performance, but they are probably a bit more advanced than the topic of this question.

One such case has to do with inner classes accessing outer class fields.

class MyOuterClass {     private String h = "hello";      // because no access modifier is specified here      // the default level of "package" is used     String w = "world";       class MyInnerClass     {         MyInnerClass()         {             // this works and is legal but the compiler creates a hidden method,              // those $access200() methods you sometimes see in a stack trace             System.out.println( h );               // this needs no extra method to access the parent class "w" field             // because "w" is accessible from any class in the package             // this results in cleaner code and improved performance             // but opens the "w" field up to accidental modification             System.out.println( w );          }     } } 
like image 147
pjco Avatar answered Sep 24 '22 08:09

pjco


well, one important point is that defining variables as private is the standard in java programming. So calling directly variables on objects will at least appear strange for other people that may possibly read your code.

One other thing I'd say is that if you are not alone coding on a project is always a good practice to limit the visibility of the attributes that are key on the class implementation to avoid strange work around that other developers may come up with.

I personally don't know if those modifiers are used to compiling and optimization purpose.

to conclude as I think every experienced java coder I strongly sujest to use this pattern in the definition of attributes.

like image 28
Mario Lenci Avatar answered Sep 22 '22 08:09

Mario Lenci