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 966139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:06:34+00:00 2026-05-16T02:06:34+00:00

Possible Duplicate: Property and Encapsulation NEWB Alert!! I am starting with Android and Java

  • 0

Possible Duplicate:
Property and Encapsulation

NEWB Alert!!

I am starting with Android and Java and I am starting to understand it but I am wondering why I should use getters and setters and not just public variables?

I see many people make a private variable and create a get and set method.

What is the idea here?

  • 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-05-16T02:06:34+00:00Added an answer on May 16, 2026 at 2:06 am

    Its called encapsulation and the concept is central to object oriented programming. The idea is that you hide the implementation of your class and expose only the contract i.e. hide the how and only expose the what. You hide the variables by making them private and provide public setters-getters and other public methods which the clients invoke to communicate with your class. They are not tied to the actual implementation of the methods or how you store your variables.

    For example, suppose you had this class where you stored a phone number as a Long object:

    public class ContactInfo {
        private Long phoneNo;
        public Long getPhoneNo() {
            return phoneNo;
        }
        public void setPhoneNo(Long phoneNo) {
            this.phoneNo = phoneNo;
        }
    }
    

    Since the clients of the class only see the getter/setter, you can easily change the implementation of the class/methods by switching the phone number representation to a PhoneNumber object. Clients of ContactInfo wouldn’t get affected at all:

    public class ContactInfo {
        private PhoneNumber phoneNo;
        public Long getPhoneNo() {
            return phoneNo.getNumber();
        }
        public void setPhoneNo(Long phoneNo) {
            this.phoneNo = new PhoneNumber(phoneNo);
        }
    }
    public class PhoneNumber {
        private Long number;
        public PhoneNumber(Long number) {
            this.number = number;
        }
        public Long getNumber() {
            return number;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: what is the difference between match_parent and fill_parent property in android FILL_PARENT
Possible Duplicate: Handling InterruptedException in Java I wonder how InterruptedException should be properly handled.
Possible Duplicate: declare property as object? in java you can create an object directly
Possible Duplicate: How to reference another property in java.util.Properties? look at my file.properties: key1=
Possible Duplicate: Can i set property of Textview like justify? I'm writing an android
Possible Duplicate: Property Name and need its value Hi, This one should be easy
Possible Duplicate: How to reference another property in java.util.Properties? I want to refer to
Possible Duplicate: Html validation error for property attribute According to facebook to use their
Possible Duplicate: Is a CSS property starting with a hash (#) valid? I tried
Possible Duplicate: Pick random property from a Javascript object suppose I have a javascript

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.