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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:54:15+00:00 2026-05-22T17:54:15+00:00

This is just a quick question to settle a dispute that I stumbled on

  • 0

This is just a quick question to settle a dispute that I stumbled on a while back (sorry I don’t have the link).

How I have been declaring object is as so:

class Foo {

   private Bar aBar = new Bar();

   ...

}

Now the dispute that I found says that this is bad Java. I have no idea why he would say that, but he was quite adamant. What he proposed was that all objects should be declared in the class body, but not instantiated until the constructor. Can anyone shed light on this for me? Is it indeed better to instantiate objects in the constructor?

TFYT

~Aedon

Edit 1:

I know that I used the word dispute, but I do not intend for this to be argumentative.

  • 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-22T17:54:16+00:00Added an answer on May 22, 2026 at 5:54 pm

    In most cases it doesn’t matter. My rule of thumb is:

    • If you’re going to use the same expression to initialize the variable in all constructors, and it doesn’t rely on any parameters, do it at the point of declaration.
    • Otherwise, you’re pretty much forced to do it in the constructor anyway.

    Reasoning: by initializing at the point of declaration, it’s clear that the value is going to be assigned the same way regardless of the constructor and parameters. It also keeps your constructors simpler, and free of duplication.

    Caveat: Don’t also assign the value in a constructor, as otherwise that invalidates the previous clarity 🙂

    I suggest you ask your colleague (or whatever) for concrete reasons for his claims that your current code is “bad”. I’m sure there are valid alternative points of view, but if he can’t provide any reasons, then there’s no reason to pay attention IMO.

    Another quick note – I’m assuming that none of the initializers need to do any significant work. If they do, that could be a point of confusion, especially if exceptions are thrown. In general, I don’t like my constructors doing a lot of work.

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

Sidebar

Related Questions

Just a quick regex question...hopefully I have a string that looks something like this:
Just a quick question, I'm getting the idea that you don't. Why is this,
This is just a quick question in C#. I have a scenario where I
just a quick question. I've got this table in a Word template which have
A quick question about saving data in symfony 2. I have this method (just
New to OOP with Perl, and just had a quick question. I have this
I just have a quick question to see if this is possible with the
just a quick question. First of all, let me verify that I have the
This is just a quick Question. I am writing a feature here that creates
This is just a quick question to understand correctly what happens when you create

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.