Referencing static object created in one class throughout entire application

17,943

Solution 1

However, both object require each other to function and if I creates a new instance of one of the objects before the other is created, the local variable in the "upper" classes is set to null.

I think the answer you are looking for is a "singleton pattern". This is where you create just one instance of a class for use in other places. Here's a good link to read. Here's the wikipedia page on it with some java examples.

So your code would look something like this:

public class A {
    private final static A instance = new A();

    /* private constructor forces you to use the getInstance() method below */
    private A() {}

    public static A getInstance() {
      return instance;
    }
}

Then wherever you want to get an instance of A you would do something like:

public class B {
    private final A classA = ClassA.getInstance();
    ...
}

There is no reason why A could not also have an instance of B and call B's methods in its own methods. What you cannot do with this cross dependency is call any of the other's methods in the constructor.

In general, by the way, these patterns should be used sparingly. A better way to accomplish this is through dependency injection instead of global references. Cross injection is possible but again, it should be used sparingly. A better solution would be to refactor the classes to have linear dependencies.

Is there any concepts in Java that would reference the actual object (like a pointer) to the object as a variable instead of making a copy of the object?

Java is pass by value but the value of any Object is the reference to the object (similar to pointers in C although they are not a memory address). So if you have an instance of A and assign it to another field, that field will be the same value and will be referencing the same instance of A.

// instantiate a new instance of A
A a1 = new A();
// assign the reference to A to another variable
a2 = a1;
// they are equivalent and both reference the same object
if (a1 == a2) ...

Solution 2

When you make a reference in Java you are actually making a copy of a reference. You aren't copying Baseclass.B in your example. You're copying a reference to Baseclass.B.

In the example code you have provided, b is going to be null until Baseclass.B is defined. If you need to do an operation on b, you can't do it in the declaration of ClassA. You need to do it in a method that is called after object a has been created.

Solution 3

Is there any concepts in Java that would reference the actual object (like a pointer) to the object as a variable instead of making a copy of the object?

Actually, Java has only references. A variable can't contain an object, so no worries there.

Also, instead of doing

private ClassB b = Baseclass.B;

I'd suggest you consider doing a static import:

import static some.package.Baseclass.*;
Share:
17,943
Chrizmo
Author by

Chrizmo

Updated on June 22, 2022

Comments

  • Chrizmo
    Chrizmo almost 2 years

    I have a java application that creates two static objects in a base class, these objects needs to references throughout classes in the program.

    public class Baseclass{
        public static ClassA A = new ClassA();
        public static ClassB B = new Classb();
        ...
        ...
        ...
    }   
    

    These objects are referenced in the other classes as a local private variables.

    public class ClassA{
        private ClassB b = Baseclass.B;
    

    However, both object require each other to function and if I creates a new instance of one of the objects before the other is created, the local variable in the "upper" classes is set to null. Is there any concepts in Java that would reference the actual object (like a pointer) to the object as a variable instead of making a copy of the object?