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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:28:51+00:00 2026-05-20T03:28:51+00:00

I am working on a Java project after a period using C++ and C#,

  • 0

I am working on a Java project after a period using C++ and C#, and I have a doubt about best practices for field initialization in constructors. Basically, suppose I have a simple Point class. In C++ my field initialization in the constructor would look like:

class Point {
public:
    // Default constructor
    Point(double x, double y) : x(x), y(Y) { }

protected:
   // Coordinates
   double x, y;
};

In C# …

class Point {
    // Coordinates, with automatic properties
    public double X { get; protected set; }
    public double Y { get; protected set; }

    // Default constructor
    Point(double x, double y) {
        X = x;
        Y = y;
    }    
}

In Java … best practices would suggest to define getters / setters for fields which must be accessed from the outside. But it is advisable to use them from inside the class as well? The doubt comes from the fact that Eclipse seems comfortable with converting each this.field = field in the class code to setField(field) for fields which have getters / setters, even if I reading / writing happens from inside the class code (therefore I wouldn’t need to use the class interface).

This basically adds up a function call for each access. Now, apart from the case in which setting a field involves some other operation (i.e. validations, processing, etc.) has this any sense? Common sense would suggest that using getters / setters is similar to using C# properties, but here I am specifically questioning about C#’s automatic properties which only involve basic access, without any processing. So the question is: is there any good in calling getters / setters with no additional processing from inside the class code?

Thank you
Tunnuz

  • 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-20T03:28:52+00:00Added an answer on May 20, 2026 at 3:28 am

    Getters Setters encapsulates the things. Today there is nothing to process but tomorrow you may need to process something. so its better to use Getters Setters.

    For default value initilization you can use constructor.\ but if there is some processing while setting getting use Getters Setters.

    Best practices is to use

    • Constructor / Initilizer Block to initilize member fields to its value.
    • from inside the class access fields directly
    • from outside class use getters/setters

    Also See

    • when-are-getters-and-setters-justified
    • java-are-getters-and-setters-evil?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project and have a question about Java sockets. The
I am working on a Java project using Netbeans for the first time. I
I have been working on a Java project for a class for a while
I am working in Java on a fairly large project. My question is about
I'm working on a project where we're using a Java applet for part of
I'm currently working on a hobby project, written in Java, containing about two different
I have a java project. The working folder from someone else's Eclipse project (It
I'm working on an app engine project (java). I'm using the jdo interface. I
I'm working on a java project that receives midi events from midi hardware using
I'm working on a Java project in which I have the getter method below

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.