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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:47:43+00:00 2026-06-07T16:47:43+00:00

So I have a question on setter and getter methods and how useful they

  • 0

So I have a question on “setter” and “getter” methods and how useful they are or aren’t.

Let’s say I just write a very basic program like the following:

    public class Account
    {
     String name;
     String address;
     double balance; 
    }

Then, let’s say I write another class that uses this “Account” class, like the following:

    class UseAccount
    {
        public static void main(String[] args)
        {
            Account myAccount = new Account();
            Account yourAccount = new Account();

            myAccount.name = "Blah blah"
        }        
    }

etc., etc.

When I write myAccount.name = "Blah blah", I am changing the value of the variable “name” in the “Account” class. I am free to do this as many times as I like with the code written the way it is. It has come to my attention, however, that it’s better practice to make the variables in the “Account” class private, and then use “setter” and “getter” methods. So if I write the following:

    public class Account
    {
       private String name;
       private String address;
       private String balance;

       public void setName(String n)
       {
          name = n;
       }

       public String getName()
       {
          return name;
       }
    }

I can still change the value of the variable “name” by just creating another class that has something like:

    class UseAccount
    {
       public static void main(String[] args)
       {
          Account myAccount = new Account();

          myAccount.setName("Blah blah");
       }
    }

I don’t understand how using this method is any different or is supposed to prevent people form changing the value of a private field. Any help?

  • 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-07T16:47:45+00:00Added an answer on June 7, 2026 at 4:47 pm

    Try this Golden Rule of Object Oriented Programming.

    1. Create private Instance variables.

    2. Create public getters and setters to access those Instance variable.

    3. This methodology is called Encapsulation. Though Encapsulation can be used in a different
    way
    , that has importance in Design Patterns, Like Those Behaviors which keeps changing must
    be encapsulated in an Abstract Class or Interfaces.

    4. Well now back to the topic of Getter and Setter….

    Getter and Setter helps Validating the Input to the Instance Variable.

    For eg:
    Assume i got a method to assign the Age of the Dog, Now the age can NoT be negative, If i dont have setter method, then i will not be able to Validate the Input of age.

    private int age;
    
    public void setDogAge(int age){ 
        if (age>0){
            this.age = age;
        } 
        else{
            System.out.println("Please Enter a Valid Age");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very basic question regarding properties in Objective-C. I can only access
I have a very basic question, when does java.util.Set checks if the objects being
I have question about XSLT1.0. The task is to write out in HTML all
Possible Duplicate: Convention question: When do you use a Getter/Setter function rather than using
Let's say we have 3 classes: A, B and C. Each class has the
Let's say I have a C# type : class MyType<T> { T Value {
I have a question regarding the correct way of splitting objects. Say I have
I'm just learning to program and have decided to try Ruby. I'm sure this
This is basically a 'best practices' question. Struts 1 forms have getters and setters
I have question about parsing in Html helper : I have sth like: @foreach

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.