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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:47:51+00:00 2026-05-14T15:47:51+00:00

I always wonder about the best way to access a class attribute from a

  • 0

I always wonder about the best way to access a class attribute from a class method in Java.

Could you quickly convince me about which one of the 3 solutions below (or a totally different one :P) is a good practice?

public class Test {

    String a;


    public String getA(){
        return this.a;
    }

    public setA(String a){
        this.a = a;
    }

    // Using Getter
    public void display(){

        // Solution 1
        System.out.println(this.a);

        // Solution 2
        System.out.println(getA());

        // Solution 3
        System.out.println(this.getA());
    }


    // Using Setter
    public void myMethod(String b, String c){

        // Solution 1
        this.a = b + c;

        // Solution 2
        setA(b + c);

        // Solution 3
        this.setA(b + c);
    }
}
  • 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-14T15:47:52+00:00Added an answer on May 14, 2026 at 3:47 pm

    That entirely depends on what the getters and setters are doing. If they do more than just getting and setting the value (which should be explicitly documented in the method’s Javadoc), then it would really make difference what way you’d choose from inside the class. But if they are pure Javabean like getters/setters, then I’d rather access the variable directly by either a or this.a depending on whether there’s a local variable in the scope with exactly that name.

    Personally I would just keep the getters and setters “pure” according the Javabean spec and add another getter or setter with a self-explaining method name whenever I’d like to do something more than just getting/setting the value. E.g. getAndIncrement(), getAsString(), setAsInt(String), etc.

    Matter of taste. It won’t really harm as long as you’re consistent with it throughout your coding.

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

Sidebar

Related Questions

I always wonder, should we use NSClassFromString before using any built in class in
I wonder, using Eclipse's PyDev plugin , how come documentation does not always show
I am learning parallel programming by myself. I wonder if distributed memory is always
I always thought that you could use OR in a LIKE statment to query
We always alloc before init in Objective C, then while writing init method, Why
People always talk about how objects created without the new keyword are destroyed when
This is a quite superfluous and uninteresting question, I'm afraid, but I always wonder
I ran across some very interesting code that makes me wonder about what bool
this may not be an earth-shattering deficiency of python, but i still wonder about
After reading about Mapper XMLs I can't help to wonder how one might go

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.