Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:30:02+00:00 2026-06-11T16:30:02+00:00

inside of a single activity, when defining components to be used only within that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:30:03+00:00Added an answer on June 11, 2026 at 4:30 pm

    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 ); 
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two <fieldset> s inside a single div (nothing else), that are positioned
I noticed that when I put an EditText (or two) inside a single TableLayout
I'm working on a .net solution that is run completely inside a single network.
The C++ standard dictates that member variables inside a single access section must be
I need to update two tables inside a single transaction. The individual queries look
I'm trying to store several 2D sprites inside a single texture, since I figured
I'm programming this game in OpenGL, mostly working inside an single EAGLView (I'm not
Is is possible to trigger a link to select a single line inside textarea.
Can we add a single column of a JTable inside a scroll pane?
I'm trying copy a single file from the Plugin directory inside of my Wordpress

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.