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

  • Home
  • SEARCH
  • 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 7799201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:13:55+00:00 2026-06-02T00:13:55+00:00

The following code illustrates the situation: class Human { private String heart = default

  • 0

The following code illustrates the situation:

class Human {

 private String heart = "default heart";

    public void control(Human h) {

            h.heart = "$%^&*@@!#^";
      }

   public String getHeart() {
    return heart;
    }
  }

 public class HumanTest {

    public static void main(String[] args) {

    Human deb = new Human();
    Human kate = new Human();

    System.out.println(deb.getHeart());
    kate.control(deb);
    System.out.println(deb.getHeart());

   }

 }

Here heart [private variable] of deb got modified unfortunately. 🙂

Java allows the code to run without any error.But is it justified to give a object the privilege to access private member of other object even if the code is in the same class ?
Shouldn’t Java disallow this?

As I know, private means restricting access beyond the class source code. But the same concept is applied in the source code above. and the result is disastrous since a person’s heart can’t be modified by any random person .

  • 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-02T00:13:57+00:00Added an answer on June 2, 2026 at 12:13 am

    If the result is disastrous, you shouldn’t code the class so that it allows it. The “bug” is not caused by code external to the class, but by code of the class itself. So it’s simply a bug in your code.

    If Java did not allow it, you could only compare objects of the same class by their public attributes, for example, which would either break encapsulation (by exposing private stuff), and/or be very slow (by forcing to make defensive copies of the private attributes to make them available to other objects.

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

Sidebar

Related Questions

The following code causes a NullReferenceException tStartParameter = String.Format(tStartParameter, tTo, tSubject) tProcess = Process.Start(New
A Java method call may be parameterized like in the following code: class Test
The following code fragment does nothing, but illustrates the problem. It was extracted from
I am in the following situation: I am writing code for a kernel that
The following code illustrates an object literal being assigned, but with no semicolon afterwards:
The following code snippet illustrates a memory leak when opening XPS files. If you
I displaying multiple p:datatable 's through ui:repeat , the following code snippet illustrates what
The following code illustrates a pattern I sometimes see, whereby an object is transformed
The following C code illustrates a problem I'm seeing on Linux 2.6.30.5-43.fc11.x86_64: #include <sys/types.h>
I have the following objects: public class User { int Id { get; set;

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.