0

Suppose I want to have a bunch of private static objects in a class, but they are complicated objects and I would like to use a specific function to initialize them with some parameters. Ideally, I would write code like this:

public class TestClass{
    private Class ComplicatedObject{
        private int anInteger;
        private String aString;
        public ComplicatedObject(int inputInt,String inputString){
             anInteger = inputInt;
             aString = inputString;
        }
        public void someMethod(){
            //do a thing
        }
    }
    private void MakeAThing(ComplicatedObject theThing, int someInt,int someString){
        theThing = new ComplicatedObject(someInt,someString);
        //do extra stuff that one might want to do here
    }
    private static ComplicatedObject IMPORTANT_OBJECT;
    public TestClass(){
         MakeAThing(IMPORTANT_OBJECT, 0,"Test");
         IMPORTANT_OBJECT.someMethod();
    }
}

This will crash because (as far as I understand Java) when I call someMethod() on IMPORTANT_OBJECT, IMPORTANT_OBJECT is actually null - the MakeAThing method did create a new object, but only its internal reference (theThing) actually referenced the new object. The reference for IMPORTANT_OBJECT is still null.

Is there any way I can write a method that will change the reference for IMPORTANT_OBJECT to reference a new object?

(yes, I know that one easy solution would be to just say IMPORTANT_OBJECT = new Object(); and then add the parameters later, but this will make my code really messy (there are many "important objects") and if there is another way I'd much prefer it.)

5
  • There is no constructor for Object that has parameters. Your question is very unclear. Please update your question to include a minimal reproducible example. I.e. code that actually compiles. Commented Apr 8, 2017 at 19:15
  • 2
    private static IMPORTANT_OBJECT; You need to define a type at compilation, otherwise it won't compile. Commented Apr 8, 2017 at 19:16
  • This code should not compile, cause there is not datatype of IMPORT_OBJECT Commented Apr 8, 2017 at 19:23
  • Instead of theThing, change it to IMPORTANT_OBJECT and remove the first parameter of TestClass#MakeAThing. Commented Apr 8, 2017 at 19:29
  • Are these arguments to MakeAThing going to not depend on anything in the TestClass constructor like in your example? If so, why not just make MakeAThing return the object and just initialize IMPORTANT_OBJECT directly in the declaration: private static ComplicatedObject IMPORTANT_OBJECT = MakeAThing(...);. If the arguments depend on something in the TestClass constructor, then how do you know that two invocations of the TestClass constructor won't give different arguments? Commented Apr 10, 2017 at 7:32

1 Answer 1

1

How about function that return new ComplicatedObject:

private ComplicatedObject MakeAThing(int someInt,int someString){
    return new ComplicatedObject(someInt,someString);
}

And just initialize the IMPORTANT_OBJECT in TestClass constructor

public TestClass(){
     IMPORTANT_OBJECT = (0,"Test");
     IMPORTANT_OBJECT.someMethod();
}

Or have I misunderstood the question?

Sign up to request clarification or add additional context in comments.

1 Comment

That works, sort of. Part of the issue was that there would be cases where IMPORTANT_OBJECT was already initialized and didn't need to be initialized again, but that could be solved by something like if (null!=IMPORTANT_OBJECT) return IMPORTANT_OBJECT;.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.