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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:28:35+00:00 2026-06-01T05:28:35+00:00

as you can see the class below declares 2 private instance variables and 2

  • 0

as you can see the class below declares 2 private instance variables and 2 get & 2 set methods associated with each private member to allow for manipulation and validation of them.

My question is: which is better to use in the constructor deceleration, the instance variables directly as shown in the snippet below or to use the set methods associated with them and also which is promote good software practices to use in the toString method, the instance variables or their getter methods?

thank you for your time.

public Class Employee {

  private String firstName;
  private String lastName;

  public Employee (String first, String last)
  {
    firstName = first;
    lastName = last;
  }//end of constructor

  public void setFirstName(String first)
  {
    firstName = first;
  }//end of method setFirstName

  public String getFirstName()
  {
    return firstName;
  }

  public void setLastName(String last)
  {
    lastName = last;
  }//end of method setLastName

  public String getLastName()
  {
    return lastName;
  }//end of method getLastName

  public String toString()
  {
    return String.format ("%s: %s %s\n", "Employee Name: ", firstName, lastName);
  }//end of method toString

}//end of class Employee
  • 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-01T05:28:37+00:00Added an answer on June 1, 2026 at 5:28 am

    I tend to favour initialisation via construction. By using this method and providing appropriate checks (compilation time via final and runtime via null checks or similar) you can ensure your object is properly and completely instantiated.

    Using the final keyword on your fields will make the compiler check that you’ve assigned a value to that field upon construction. It does mean that field is immutable, but it’s surprising how often you require that.

    I would be careful with providing getters for everything. If you’re continually providing and using getters, that suggests that you’re pulling data out of your object and manipulating it outside that object. Remember – a key principle of OO is getting objects to do things for you, rather than asking them for data and doing it yourself.

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

Sidebar

Related Questions

Why can't I set $_SERVER['DOCUMENT_ROOT'] as attribute? see example code class foo { private
I have two async classes in my application which you can see below class
How can declare template of a template class?? see below code: File: A.h class
As you can see in the code below, I have an Abstract Base Class
As you can see from the Client class, if the while breaks then the
I want see the source code of System.Web.Routing.RouteValueDictionary class. But Reflector can't disassemble it
Below you can see my .xaml.cs code. The app opens fine. There are 4
See the code below. The drive() is in the scope , I can drive
Hey, I'm wondering how I can get the class names dynamically using jquery for
I have the following method (below), as you can see it serializes an object

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.