28

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?

Octoth0rpe
  • 2,267
  • 4
  • 19
  • 21
  • 2
    For the most part, especially in the scenario you describe where everything is in just one class/activity, its just considered good form to limit the scope any variables you use. – Finding Nemo 2 is happening. Sep 19 '12 at 18:53

5 Answers5

31

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 ); 
        }
    }
}
pjco
  • 3,826
  • 25
  • 25
4

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.

Mario Lenci
  • 10,422
  • 5
  • 39
  • 50
2

The scope of visibility has nothing to do with the garbage collector or memory management

You will want to reduce the scope of visibility as much as possible so your code can be easier to maintain.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • Thanks man, Digging around some more and it looks like this is the convention: No difference in garbage collection or memory management. private it is. – Octoth0rpe Sep 19 '12 at 19:17
1

private and public are both keywords of Java that have the purpose of Object Orientated Design. I suggest you read up about this: http://docs.oracle.com/javase/tutorial/java/concepts/

If you are only going to use those variables (objects) in your activity, then I would suggest you make those variables private.

I hope this helps.

Edit:

I'm not sure if using the private, public or no keyword will optimize you app from a memory point of perspective. As far as I can tell I think it does not and you should use what makes your code most readable, intuitive and maintainable.

Luke Taylor
  • 9,481
  • 13
  • 41
  • 73
  • Thanks, I know what they do, and I know they don't NEED to be pubic in my case, but I was wondering if there was any sort of voodoo Memory Management or Garbage Collection that's affected by whether or not something is default, public or private. – Octoth0rpe Sep 19 '12 at 19:10
0

If your variable declaration is inside the Activity's scope, it acts normally as a scoped variable normally would.

It is, however, bad programming practice to use variables from one method in another method when they're not parameters.

Example:

Bad:

void Foo()
{
    int foo = 5;
    System.out.println(Bar());
}

int Bar()
{
    return foo + 5;
}

This will actually throw a syntax error because foo is declared outside of scope for Bar()

Good:

int foo;

void Foo()
{
    foo = 5;
    System.out.println(Bar(foo)); //prints 10
}

int Bar(int foo)
{
    return foo + 5;
}
Codeman
  • 12,157
  • 10
  • 53
  • 91
  • @Alex because you want to avoid (as a design principle) unnecessary side effects. A side effect is defined as something that affects information outside of your current context. – Codeman Sep 19 '12 at 19:50
  • 2
    Your 'Bad' doesn't compile therefore it's not bad, rather it doesn't exist. Therefore Good is not compared with anything therefore I wonder why it's good. – Alexander Kulyakhtin Sep 19 '12 at 20:28