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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:35:19+00:00 2026-05-25T22:35:19+00:00

I’m newbie to Java and I’m learning about encapsulation and saw an example where

  • 0

I’m newbie to Java and I’m learning about encapsulation and saw an example where instance variables are declared as private in a class.

http://www.tutorialspoint.com/java/java_encapsulation.htm

I have 2 queries:

  1. Why are instance variables private? Why not public?
  2. What if instance variables are made public and accessed directly? Do we see any constraints?

Can you explain with an example as to what will go wrong in case the instance variables are declared as public in a class in Java?

  • 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-25T22:35:19+00:00Added an answer on May 25, 2026 at 10:35 pm

    Instance variables are made private to force the users of those class to use methods to access them.
    In most cases there are plain getters and setters but other methods might be used as well.

    Using methods would allow you, for instance, to restrict access to read only, i.e. a field might be read but not written, if there’s no setter. That would not be possible if the field was public.

    Additionally, you might add some checks or conversions for the field access, which would not be possible with plain access to a public field. If a field was public and you’d later like to force all access through some method that performs additional checks etc. You’d have to change all usages of that field. If you make it private, you’d just have to change the access methods later on.

    If phone was private:

    Consider this case:

    class Address {
      private String phone;
    
      public void setPhone(String phone) {
        this.phone = phone;
      }
    }
    
    //access:
    Address a = new Address();
    a.setPhone("001-555-12345");
    

    If we started with the class like this and later it would be required to perform checks on the phoneNumber (e.g. some minimum length, digits only etc.) you’d just have to change the setter:

    class Address {
      private String phone;
    
      public void setPhone(String phone) {
        if( !isValid( phone) ) { //the checks are performed in the isValid(...) method
         throw new IllegalArgumentException("please set a valid phone number");
        }
    
        this.phone = phone;
      }
    }
    
    //access:
    Address a = new Address();
    a.setPhone("001-555-12345"); //access is the same
    

    If phone was public:

    Someone could set phone like this and you could not do anything about it:

    Address a = new Address();
    a.phone="001-555-12345";
    

    If you now want to force the validation checks to be performed you’d have to make it private and whoever wrote the above lines would have to change the second line to this:

    a.setPhone("001-555-12345");
    

    Thus you couldn’t just add the checks without breaking other code (it wouldn’t compile anymore).

    Additionally, if you access all fields/properties of a class through methods you keep access consistent and the user would not have to worry about whether the property is stored (i.e. is a instance field) or calculated (there are just methods and no instance fields).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.