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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:24:53+00:00 2026-05-26T17:24:53+00:00

I’d like to know what’s the default approach for those times when you need

  • 0

I’d like to know what’s the default approach for those times when you need a variable to have been set in order for a given method/another variable initialization to work.

Like this:

Everything works if I initialize var A after var B. But not the other way around. I wrote the constructor, so I’ll do that myself, but I’m not really sure where the code that tests for var B‘s existence should be. Or even if it should exist at all, for I have written the constructor and I initialize the values the order I see fit, but I feel it’s a little insecure because it is not very robust in case anything changes.

Mind you, I’m talking about instance variables, if that helps.

FA

  • 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-26T17:24:53+00:00Added an answer on May 26, 2026 at 5:24 pm

    The answer can be influenced by the reason why a must be set before b.

    Explicit Object Dependencies

    If the reason is that b depends upon a, then the simplest thing to do is to make that dependency explicit at the time that b is created. For example, if a and b were objects then:

    var a = new A(...);
    var b = new B(a, ...);
    var op = new Operation(b);
    op.perform();
    

    In this way, it is not possible to initialize the objects out of order. Note that A and B could be newly introduced wrapper objects that contain the original operation parameters.

    Fluent Interface

    If the reason is that the operation itself must know the value of a in order to perform some configuration in preparation for the arrival of b, then the operation constructor could be replaced by a fluent interface:

    Operation op = Operation.withA(a).withB(b);
    op.perform();
    

    We must take care to define this fluent interface in such a way that withB can only be called after withA has been called. For example:

    public class Operation {
    
        private final C _c;
        private final B _b;
    
        private Operation(C c, B b) {
            _c = c;
            _b = b;
        }
    
        public static BStep withA(final A a) {
            return new BStep() {
                public Operation withB(B b) {
                    C c = setUpStateDependentUponA(a);
                    return new Operation(c, b);
                }
            };
        };
    
        public interface BStep {
            Operation withB(B b);
        }
    
        public void perform() {
            // do something with _c and _b
        }
    
    }
    

    Here, C has been introduced to capture that state that is dependent upon a alone prior to the arrival of b. Note how the constructor of Operation is not visible to client code and that withB cannot possibly be called until after withA has been called.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have two tables with like below codes: Table: Accounts id | username |
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace

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.