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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:18:23+00:00 2026-05-27T10:18:23+00:00

My professor really emphasizes protecting against privacy leaks by always using accessors and mutators

  • 0

My professor really emphasizes protecting against privacy leaks by always using accessors and mutators to access private instance variables; however, do I have to use the getters/setters of a class within the class?

So for instance, if I have the following class:

public class Person 
{
    private String name;
    private int age;
}

and I want to write a toString() method for it. Can I just write:

public String toString()
{
    return name + " " + age;
}

OR do I need to do something like this:

public String toString()
{
    return this.getName() + " " + this.getAge();
}
  • 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-27T10:18:23+00:00Added an answer on May 27, 2026 at 10:18 am

    You CAN do either one. However, your professor might appreciate using the methods instead of the direct access. Here’s why.

    Let’s say you have a class like this:

    class SomeClass {
        private int someValue;
        private String someString;
    
        public SomeClass(int someValue, String someString) {
            this.someValue = someValue;
            this.someString = someString;
        }
    
        public int someValue() {
            return this.someValue;
        }
    
        public String someString() {
            return this.someString;
        }
    
        public String toString() {
            return someValue + ": " + someString;
        }
    
    }
    

    It’s pretty straightforward, right? Well, what if all of a sudden we want to CHANGE the implementation of how we calculate someValue, and base it off of someString:

    public int someValue() {
        int value = 0;
        for(int i = 0; i < someString.length; i++) {
             if(someString.charAt(i) == ' ') value++;
        }
        return value;
    }
    

    Now you also have to change every place where variable someValue was used.

    So if you want to make the code easier to maintain in the long run, use the methods calls. This way when you code changes on you (and trust me, it changes all the time) you only have to change it in one spot instead of two.

    And yes, you would want to use a method call in getting someString instead of the direct access in the last method 🙂

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

Sidebar

Related Questions

I've gotten really rusty with TSQL I'm ashamed to say. Using Entity Framework has
My professor assigned a project where a simulation is ran through a GUI. To
My professor provided me with a file called CursorList.cpp that implements a Cursor Linked
I am taking a class in C++ programming and the professor told us that
I have been taking a few graduate classes with a professor I like alot
So I'm working on rewriting a program for a professor, and I have some
In my Computer Science II class, the professor considers ++,--,*=, etc. to be 2
I'm currently taking a math class in College called Scientific Computing and the professor
I am an adjunct professor teaching database and programming classes. I own a desktop
Today in the class of Java the professor came up with this example but

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.