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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:04:47+00:00 2026-06-12T00:04:47+00:00

I have taken to using setters instead of putting arguments into the default constructor

  • 0

I have taken to using setters instead of putting arguments into the default constructor because it helps me organise my code better

The problem is that the only variable on a project I am doing is a String and I am not sure whether I should be initializing it in the declaration (as a global variable?), in an setter instance method or whether to initialise it in the class constructor.

I am wondering if there could be anything problematic about this set up whether the instance is not initialised until it’s setter is used:

class MyClass{

  private String myString; 

  public MyClass(){

  }
  public void setStuff(String s){ 

    this.myString=s;
  }
}
  • 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-12T00:04:48+00:00Added an answer on June 12, 2026 at 12:04 am

    Not sure what you mean by global variable? Is it truely a variable that is the same inrespective of the object that is created? If so it should be static

    private static String myString = "str";
    

    If it never changes then it should be final and is then a global constant

    public static final String MY_STRING = "str";
    

    This can then be public and accessed via

    MyClass.MY_STRING
    

    If it is only applicable for each object of the class that is created then either initialise it in the constructor or in the declaration

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

Sidebar

Related Questions

I have Created a PieChart Using the following Code. Here, I Have taken the
In pom.xml I have (taken from http://struberg.wordpress.com/2012/05/10/using-jpa-in-real-projects-part-1/ ): <properties> <openjpa.sql.action>refresh</openjpa.sql.action> <database.driver.name>org.postgresql.Driver</database.driver.name> <database.connection.url>jdbc:postgresql://myURL</database.connection.url> <database.user>user</database.user> <database.password>password</database.password>
I am using a DataGrid from the WpfToolkit. I have taken the Style resource
I have an array that is taken from an image using exif_read_data($image, 0, true)
I have a PHP function that takes a variable number of arguments (using func_num_args()
I have taken this little snippet straight out of some code I'm working on:
I am using a WPF ribbon and I have just run into some strange
Using nl.siegmann.epublib I have taken a resource from a book which I try to
I'm using .net 2.0. This is a project that I have taken over for
I have got access_token. Using zend framework. I have obtained token for multiple permissions.

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.