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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:02:03+00:00 2026-05-16T16:02:03+00:00

I was doing a project in a Java book and came across this code

  • 0

I was doing a project in a Java book and came across this code example. The author of the book said that instead of initializing X and Y in my constructor directly, I could call the class’s setLocation() method instead. Unfortunately I do not have the book anymore for a concrete explanation on why this is better. I’m not too experienced with Java but isn’t it just…simpler to assign values directly and not worry about another function call?

//Point constructor, normal way of initializing variables

private double x;
private double y;

Point(double initial_x, double initial_y)
{
 this.x = initial_x;   
 this.y = initial_y;
} 

//Point constructor, the other way

Point(double initial_x, double initial_y)
{ 
   setLocation(initial_x, initial_y);
} 


public void setLocation(double newX, double newY)
{
   this.x = newX;
   this.y = newY; 
}
  • 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-16T16:02:04+00:00Added an answer on May 16, 2026 at 4:02 pm

    I would actually recommend against calling setters within a constructor, because calling overridable methods from within constructors is a bad idea. Also, the usual argument for getters/setters is that the logic may change, and you won’t have to change everyone that accesses your properties. In this case, however, if the logic changes, the necessary changes are limited to the same class.

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

Sidebar

Related Questions

I'm doing a Java software-project at my university that mainly is about storing data-sets
I have a Java project that uses this driver for serial communication. The driver
I have a postgresql database that has this column structure: Author id name Book
I am doing this AntFarm project for my Java class. This project consists of
I am doing project using Java and in that I need to reload whole
I have a Java project that builds using Apache Ant to a website. This
I'm doing another Java project for my class. In this assignment, we have to
I am using a Java Project that gives an user interface for doing some
I am doing JAVA project based on the SSJ (Stochastic Simulation in Java) libraries.
I'm doing project on object detection using javacv in that I went through couple

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.