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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:36:36+00:00 2026-05-31T22:36:36+00:00

I am just learning and would like to know about a piece of code

  • 0

I am just learning and would like to know about a piece of code that sets the object variable.

What is the correct way to set object variable bfield in the follwoing test class?

public class test {
private String afield;

private String bfield;

public test() {

buildList();

}

public void buildList() {

    some code to derive and populate afield.

    this.bfield = this.afield;   //  ( 1)

    setBfield(afield);  // (2) say getter and setters do exist

    bfield = afield;  //  (3)
}

What is the right way to do? I soption 1 OK or option 2?

  • 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-31T22:36:37+00:00Added an answer on May 31, 2026 at 10:36 pm

    Any of the three will work, of course.

    I generally don’t like option 1, unless i’m differentiating between an instance member and an argument. For example, public void buildList(String bfield) { this.bfield = bfield; }. this.everything is extra noise; if you don’t need it, all it does is give the bugs more code to hide in. 🙂

    Option 2 is more future-proof; if ever you change things so that something else has to be set along with bfield (or if bfield doesn’t need a backing field at all — for instance, if setting it should set something on a sub-object), you’ll be glad you called setBfield — cause you won’t have a dozen places to change code that sets bfield. Basically, if you need and already have a setBfield method, i’d recommend using it in most cases.

    If you have a field you know will always be contained within the object itself, and is independent of other fields, option 3 is typically faster. Plus, you don’t have to create a setter (read: pollute your interface), if you don’t want outside code to be able to set bfield as well.

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

Sidebar

Related Questions

I am learning about Java Bytecodes. I would like to know if I am
for learning purposes I would like to know how to implement an image (and/or)
Hello I am just learning more about using classes in PHP. I know the
I just read some basics about Molehill, and I understood that learning an API
I am just learning about app.config in respect of creating custom sections. I have
I'm just learning about SVG, and it seems great but I'm not sure about
I've just started learning about extending the Configuration file structure by creating custom section
I just get a lil (xD, a lot would be better) worried about sanitizing
I am kind of oldskool and just getting back to learning about all the
Just learning Silverlight 4/RIA and i 'm stuck in a weird problem: setup an

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.