Link to a static object created in one class throughout the application

I have a Java application that creates two static objects in a base class, these objects must refer to all classes in the program.

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

These objects are referred to in other classes as local private variables.

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

However, both objects require functions from each other, and if I create a new instance of one of the objects before the other is created, the local variable in the "upper" classes will be set to zero. Are there any concepts in Java that would refer to an actual object (like a pointer) to an object as a variable instead of creating a copy of the object?

+6
source share
5 answers

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

So your code will 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 cannot also have an instance of B and call methods B in its own methods. What you cannot do with this cross-dependency is to call any other method in the constructor.

In general, by the way, these patterns should be used sparingly. The best way to achieve this is to inject dependencies instead of global references. Cross-injection is possible, but again, it should be used sparingly. A better solution would be to refactor classes with linear dependencies.

Are there any concepts in Java that would refer to an actual object (like a pointer) to an object as a variable instead of creating a copy of the object?

Java is passed by value, but the value of any object is a reference to the object (similar to pointers to C, although they are not a memory address). Therefore, if you have an instance of A and assign it to another field, this field will be the same value and will refer to 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) ... 
+6
source

Are there any concepts in Java that would refer to an actual object (like a pointer) to an object as a variable instead of creating a copy of the object?

Actually, Java has only links. A variable cannot contain an object, so don't worry.

Also, instead of doing

 private ClassB b = Baseclass.B; 

I would advise you to consider static imports:

 import static some.package.Baseclass.*; 
+1
source

When you make a link in Java, you are actually making a copy of the link. You are not copying Baseclass.B into your example. You are copying the link to Baseclass.B .

In the code example below, b will be null until Baseclass.B is defined. If you need to perform an operation on b , you cannot do this in a ClassA . You need to do this in the method that is called after the creation of object a .

+1
source

This is a classic singlet app.

For each of them:

  • Make the constructor private.
  • Add a private static member of the native type class to store singleton.
  • Add the getThe () method, which initializes the above element, if it is not already set.

See Wikipedia: Singleton Template .

Create an A-constructor constructor of B by doing getThe () on it.

Also, do not use public fields in Baseclass ; use the public getter methods instead. Do not store separate variable B; instead, ask for singleton A.

0
source

Are there any concepts in Java that would refer to an actual object (like a pointer) to an object as a variable instead of creating a copy of the object?

When you do this:

 private ClassB b = Baseclass.B; 

Indeed, you use ponting on the same object a, because the variable "b" is called the reference variable.

About your question, my recommendation does something like this:

First encapsulate the link:

 public class ClassA{ private ClassB b; public void setB(ClassB b) { this.b = b; } public ClassB getB(ClassB b) { return this.b; } } 

Second use of a static block to initialize variables:

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

Source: https://habr.com/ru/post/902133/


All Articles