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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:28:17+00:00 2026-06-11T04:28:17+00:00

I am learning java. Now, it is always a practice in oop to make

  • 0

I am learning java. Now, it is always a practice in oop to make the fields in a class private and provide get and set methods as per the requirements to access or modify the variables. Now my question is that if you have a set method for a field that is declared private, the user can still change the value of the field even if it is invisible. So, why not make the field public instead of providing a set method?

  • 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-11T04:28:18+00:00Added an answer on June 11, 2026 at 4:28 am

    It is always a practice in oop to make the fields in a class private and provide get and set methods as per the requirements to access or modify the variables.

    Variables are best kept private (although you can keep it public). Not all variables need get and set (accessors and mutators) functions. It depends on use case. For example if I am holding the count of some sort, which is internal to the class, I would not provide a set(..) method to it. If your get(..) and set(..) methods do nothing more than returning and assigning, I would suggest you follow your company’s coding guidelines. And if you have the option go for getters and setters.

    Now my question is that if you have a set method for a field that is declared private, the user can still change the value of the field even if it is invisible. So, why not make the field public instead of providing a set method?

    A very simple/trivial example here:

    void setVar(string x){
        myPrivateVar = x;
    
        //log the change..
        writeToLog("myPrivateVar was updated to: " + x + " at <blah blah>");
    
        //inform others of the change.
        updateDependantConfiguration();
    }
    

    HTH ..

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

Sidebar

Related Questions

I am taking my first OOP class (learning Java) in the fall so I've
I'm learning java and now I have this question. I created a class named
I'm learning how to make a google appengine app in Java now, and in
I'm just now learning how to use the Android/Java Socket class and I need
I'm learning how to develop SOAP web services with Java. So far now I've
I am learning Java, now working with arrays. I am doing an excercise where
I am new to programming. I am learning Java now, there is something I
I'm learning Java right now but I'm wondering if there is a hierarchy of
I'm learning event-driven programming by Java now, but most materials are about swing. I
I am learning Java (slowly) from the ground up, but every now and again

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.