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

  • Home
  • SEARCH
  • 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 6851469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:15:06+00:00 2026-05-27T01:15:06+00:00

I have a java application that creates two static objects in a base class,

  • 0

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?

  • 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-27T01:15:07+00:00Added an answer on May 27, 2026 at 1:15 am

    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) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a Java application that needs to be brought to the foreground when
I have a classic ASP/VBScript application that needs to call methods in a Java
I'm writing a Java application that will instantiate objects of a class to represent
i have an Java J2ME application that does (at user request) create HttpConnections to
I have created a java application for Debian Linux. Now I want that that
I have created one application in flex that is accessing the Java webservice using
I have a Java application that launches another java application. The launcher has a
I have a Java application that monitors a folder for incoming XML files. When
I have a Java application that's very String-heavy - it takes a feed of
I have a java application that has Web Services published using Axis. With the

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.