How do I know if an instance of a class exists in memory?

How do I know if an instance of a class exists in memory?


My problem is that I don’t need a read method, if there is an instance of the class this is my code

private void jButton (java.awt.event.ActionEvent evt) {
    PNLSpcMaster pnlSpc = new PNLSpcMaster();
    jtabbedPanel.addTab("reg",pnlSpc);
}

I want to check the instance PNLSpcMaster, of course, I can check the static Boolean, but I think this method is better.

+3
source share
5 answers

If you want to have only one instance of "PNLSpcMaster", then you need a singleton :

This is a common single idiom:

public class PNLSpcMaster {

   /**
    * This class attribute will be the only "instance" of this class
    * It is private so none can reach it directly. 
    * And is "static" so it does not need "instances" 
    */        
   private static PNLSpcMaster instance;

   /** 
     * Constructor make private, to enforce the non-instantiation of the 
     * class. So an invocation to: new PNLSpcMaster() outside of this class
     * won't be allowed.
     */
   private PNLSpcMaster(){} // avoid instantiation.

   /**
    * This class method returns the "only" instance available for this class
    * If the instance is still null, it gets instantiated. 
    * Being a class method you can call it from anywhere and it will 
    * always return the same instance.
    */
   public static PNLSpcMaster getInstance() {
        if( instance == null ) {
            instance = new PNLSpcMaster();
        }
         return instance;
   }
   ....
 }

Using:

private void jButton (java.awt.event.ActionEvent evt) {
    // You'll get the "only" instance.        
    PNLSpcMaster pnlSpc = PNLSpcMaster.getInstace(); //<-- getInstance()
    jtabbedPanel.addTab("reg",pnlSpc);
}

Or directly:

private void jButton (java.awt.event.ActionEvent evt) {
    jtabbedPanel.addTab("reg",PNLSpcMaster.getInstace());
}

For basic conditions of use, the Singleton Pattern works very well. However, for more complex applications this can be dangerous.

:

+7
+1

, .

, :

class MyClass {
    private static bool instanceExists = false;
    public MyClass() {
        MyClass.instanceExists = true;
    }
}
0

Java, ++.

, , hasAtleastOne().

class Example {
    private static int noOfInstances = 0;

    public Example() {
        noOfInstances++;
    }


    public static boolean hasAtleastOne() {
        if(noOfInstances > 0)
            return true;
        else
            return false;
    }

    protected void finalize() throws Throwable {
        noOfInstances--;
    }
}

, Java, ++. , , - , . , , .

, , , finalize() , , . - , - ; , , .

You could add extra reliability to the solution by implementing the Dispose pattern . It also requires the class client to call the dispose method to signal that the instance should be deleted, so that the instance counter can be reduced. Poorly written clients will make the decision unreliable.

0
source

For classes that have the concept of identity, an identity card template is used .

0
source

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


All Articles