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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:38:46+00:00 2026-05-30T22:38:46+00:00

I have simple but stupid question. We use access modifiers in Java to access

  • 0

I have simple but stupid question. We use access modifiers in Java to access variables in a Class. I do not understand that if we can access a private class variable outside the class using properties defined in public class, then what is difference between public and private? It is still accessible outside private class. I do not understand the concept of this public private concept.

  • 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-30T22:38:48+00:00Added an answer on May 30, 2026 at 10:38 pm

    If I understand your question correctly, you are asking why someone would write this:

    public class MyClass {
        private int value;
        public int getValue() {
            return value;
        }
        public int setValue(int val) {
            value = val;
        }
    }
    

    instead of simply:

    public class MyClass {
        public int value;
    }
    

    The main reason is to hide the internal structure of MyClass from client code. The benefit is that this allows you to more easily modify the internal structure of MyClass. Perhaps, for instance, you decide that another field in MyClass needs to be updated whenever value changes. This would be very difficult to implement without error if accesses to value were scattered everywhere in your code (and nigh-on impossible if your code served as a library for someone else’s code).

    Additional benefits are that you can, if necessary, rewrite getValue() and setValue(int) to synchronize access to the value, to log writes to value, or do any number of other things.

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

Sidebar

Related Questions

I have a simple and probably stupid question, but I can't really find any
I have what is probably a really simple, studid question but I can't find
I have a very simple (rather stupid) question, I hope someone can clear my
This is probably a stupid question but, I have a method I use to
I have a simple but subtle question. Below you see two different declaration variants
I have a simple problem but I am not sure how to solve it.
I have a simple problem but no matter what I try I can't see
I have an annoying SQL statement that seem simple but it looks awfull. I
I think this must be simple but I can't get it right... I have
Hey guys, this is simple but I can't make it workn... blah! I 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.