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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:06:58+00:00 2026-06-11T21:06:58+00:00

When we have a setter method for private variable in java, what is the

  • 0

When we have a setter method for private variable in java, what is the purpose of making the variable private?

We are having private variables to restrict the user not to access the state of an instance directly. But in sometimes, we are having corresponding setter and getter methods to access and change the state of the private variable. If so, then why do we make the variable private? We can just have public variable instead right?

  • 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-11T21:06:59+00:00Added an answer on June 11, 2026 at 9:06 pm

    The rationale is that you’re hiding the implementation. When you call

    setX(23);
    

    your corresponding variable x is hidden from client classes and can’t be directly observed or modified.

    Why is this good ?

    1. it hides the implementation. You can change x at a later date to (say) a string, a double etc. and your client code doesn’t have to change. x could even disappear and calling setX() would actually perform some different but equivalent function.
    2. by implementing a setter function, you can enforce validation etc. Otherwise you can’t prevent someone setting x to an invalid (e.g. negative) value.
    3. (this is a Java-specific benefit rather than a general OO benefit). Lots of Java frameworks work with objects using the JavaBeans convention. i.e. for a property x there will exist a corresponding getX()/setX() (or isX() if it’s a boolean). By adhering to convention your class will interoperate nicely with these frameworks.

    Note there are arguments for not having setters but rather implementing immutable objects (so you’d set your value via the constructor alone). That makes the object thread-safe and debugging/reasoning about the object state easier.

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

Sidebar

Related Questions

I'm using Java. I want to have a setter method of one class that
I have a Class that has a private variable with a public setter/getter function:
I have seen member variables given a private modifier and then using getter/setter methods
I have a Long variable say, private Long m_prevPacketRecvdTime = null; now I have
I have the following variable in a class named Example: private static int number;
I have a variable in my backing bean of type int: private int myVariable;
I have a class defined like this, with the appropriate getter and setter methods...
I have a string array selectCancel with setter and getter methods, which is a
I have a setter like this: - (UIImagePickerController *) foto { if (_foto ==
i have a class: class MyClass { @Getter @Setter int a; @Getter @Setter int

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.