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

  • SEARCH
  • Home
  • 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 8094015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:39:28+00:00 2026-06-05T20:39:28+00:00

Possible Duplicate: Why use getters and setters? Is there any advantage to making methods

  • 0

Possible Duplicate:
Why use getters and setters?

Is there any advantage to making methods to access private variables in your class instead of making the variable public?

For example is the second case better than the first?

//Case 1
public class Shoe{
    public int size;
}

//Case 2
public class Shoe{
    private int size;
    public int getSize(){
        return size;
    }

    public void setSize(int sz){
        size = sz;
    }

}
  • 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-05T20:39:31+00:00Added an answer on June 5, 2026 at 8:39 pm

    What I have seen someday on SO, as answer (written by @ChssPly76) why to use getters and setters

    Because 2 weeks (months, years) from now when you realize that your
    setter needs to do more than just set the value, you’ll also realize
    that the property has been used directly in 238 other classes 🙂

    there are much more advantages:

    1. getters and setter can have validation in them, fields can’t
    2. using getter you can get subclass of wanted class.
    3. getters and setters are polymorphic, fields aren’t
    4. debugging can be much simpler, because breakpoint can be placed inside one method not near many references of that given field.
    5. they can hide implementation changes:

    before:

    private boolean alive = true;
    
    public boolean isAlive() { return alive; }
    public void setAlive(boolean alive) { this.alive = alive; }
    

    after:

    private int hp; // change!
    
    public boolean isAlive() { return hp > 0; } // old signature 
     //method looks the same, no change in client code
    public void setAlive(boolean alive) { this.hp = alive ? 100 : 0; }
    

    EDIT: one additional new advange when you are using Eclipse – you can create watchpoint on field, but if you have setter you need just a breakpoint, and… breakpoints (e.g. in setter method) can be conditional, watchpoints (on field) cannot. So if you want to stop your debugger only if x=10 you can do it only with breakpoint inside setter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why use getters and setters? I have read books on Java, saying
Possible Duplicate: Why use getters and setters? In C#(ASP.NET) we use getter and setter
Possible Duplicate: Why use getters and setters? Yes, It's a very simple thing but
Possible Duplicate: Why use getters and setters? I see this a fair bit in
Possible Duplicate: Why use getters and setters? I'm reading the Java for Dummies 2nd
Possible Duplicate: Is it really that wrong not using setters and getters? Why use
Possible Duplicate: How to use my own sqlite database? Full Android Database Helper class
Possible Duplicate: When to Use Double or Single Quotes in JavaScript Are there differences
Possible Duplicates: Public Data members vs Getters, Setters Purpose of private members in a
Possible Duplicate: Use of class definitions inside a method in Java Can we have

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.