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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:39:04+00:00 2026-06-03T07:39:04+00:00

Possible Duplicate: Why use getters and setters? I have read books on Java, saying

  • 0

Possible Duplicate:
Why use getters and setters?

I have read books on Java, saying that it is good to create setters and getters for variables such as x and y. For example:

public int getX(){
    return x;
}

public void setX(int x){
    this.x = x;
}

But what is the difference from that and

...(shape.x)...   // Basically getX()

and

shape.x = 90;    // Basically setX()

If setters and getters are better, what practical problems would arise?

  • 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-03T07:39:05+00:00Added an answer on June 3, 2026 at 7:39 am

    Multiple reasons:

    • If you allow field access like

      shape.x = 90

    then you cannot add any logic in future to validate the data.

    say if x cannot be less than 100 you cannot do it, however if you had setters like

    public void setShapeValue(int shapeValue){
      if(shapeValue < 100){
        //do something here like throw exception.
      }
    }
    
    • You cannot add something like copy on write logic (see CopyOnWriteArrayList)
    • Another reason is for accessing fields outside your class you will have to mark them public, protected or default, and thus you loose control. When data is very much internal to the class breaking Encapsulation and in general OOPS methodology.

    Though for constants like

    public final String SOMETHING = "SOMETHING";
    

    you will allow field access as they cannot be changed, for instance variable you will place them with getters, setters.

    • Another scenario is when you want your Class to be immutable, if you allow field access then you are breaking the immutability of your class since values can be changed. But if you carefully design your class with getters and no setters you keep the immutability intact.

    Though in such cases you have to be careful in getter method to ensure you don’t give out reference of objects(in case your class have object as instances).

    We can use the private variables in any package using getters and setters.

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

Sidebar

Related Questions

Possible Duplicate: Is it really that wrong not using setters and getters? Why use
Possible Duplicate: Use of class definitions inside a method in Java Can we have
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: When do you use Java’s @Override annotation and why? From the javadoc
Possible Duplicate: When should one use final? When should Java programmers prefer to use
Possible Duplicate: Use Visual Studio web.config transform for debugging I have an asp.net application
Possible Duplicates: Why use getters and setters? C# 3.0 Auto-Properties - useful or not?
Possible Duplicate: Use of Java [Interfaces / Abstract classes] What's the benefit of interfaces

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.