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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:31:33+00:00 2026-05-25T09:31:33+00:00

This is a theory question I guess that I am using to find the

  • 0

This is a theory question I guess that I am using to find the standard procedure for this.

If I have a Constructor method that does a whole lot of setup operations gathering data and such, should I keep “all things construction” in the constructor, or should I try to call other methods from inside the constructor (for code looks basically), or should I just initialize everything I have to and leave other things to be dealt with later if they are actually needed?

Here is an example.

I am creating an object that is a collection manager basically. It needs to read in data from a file and it stores it inside of an array.

Do I use the constructor to just create an object with base properties and read data later,
or should I read in all the info and set up the array inside the constructor which saves time later but takes up extra time here, or should I do something along the lines of

 public myConstructor(String filename) {
data = readDataIn(filename);
} 

This is not actual code, just an example of outsourcing to different methods to “pretty up the code” instead of a super long constructor method I can have say 5-6 short and good looking methods that can only be accessed by the constructor.

  • 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-25T09:31:34+00:00Added an answer on May 25, 2026 at 9:31 am

    The constructor should do just enough work to get the instance into a state that satisfies its contract. Each method should then do just enough work to fulfill the method’s contract and leave the instance in a state that satisfies its contract.

    Very rarely should a constructor call cause side-effects or modify its inputs. These are just not often required to satisfy a contract. For example, a connection class shouldn’t touch the network on construction. Since it has to be closeable, the closed state must be part of its contract, and so the “just enough work” standard dictates that the constructor puts it in a ready, but not yet open state.

    Your particular example couples your class to the file system. You would probably get a more testable, more general class by using Guava Files to do the reading and taking a string with the content instead. You can get the convenience of a constructor coupled to the file system by writing a convenient static MyClass fromFile(String path) factory function that does new MyClass. That moves the portion of your code that is coupled to the filesystem outside the portion that interacts with instance variables reducing the number of possible interactions to test. As others have noted, dependency injection is another good way to achieve decoupling.

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

Sidebar

Related Questions

This is more of a cryptography theory question, but is it possible that the
This is a simple question from algorithms theory. The difference between them is that
This is more of a theory question. If I'm running 50,000 queries that insert
I have an image that is 480x270. In theory this should occupy all the
This is more of a theory question, then any actual code. I understand that
I am sorry if this is more of a theory question then a real
This is purely a theory question, so, given an "infinite" time to make a
This is a combinatorics question with some theory in hashing algorithms required. Let's say
This question is a spin-off/evolution of this question . (That question got marked as
I have kind of a theory question on what happens when you have jQuery

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.