java - Getting a single instance of an object -
Assume that I have a class A
and the list of this class l < / Code>. There is a square
B
which increases A
which will reach the list l
, as shown below. In my program, there should be an example of class B
(I am using singleton pattern), and should be just one example of the list l
, so I want to do this I am doing:
Public abstract class A {secure list & lt; String & gt; L; Public A () {} Secure synchronized list & lt; String & gt; GetList () {if (l == faucet) l = New Linkedist & lt; String & gt; (); Returns L; }} // --- Extends the public class B {Personal Static B Example; Private B () {super (); } Public stable synchronization b getInstance () {if (example == faucet) Example = New B (); Return example; }}
What do I do:
b
:
println (.super.getList () Shape ()); // should print super.getList (). Add ("a"); Println (. Super.getList () size ()); // will print B
:
In another example .println (super.getList () .size ()); // 1 should print because I already added // "a", but prints 0 super.getList (). Add ("b"); Println (. Super.getList () size ()); // print 2, but print 1
is not working as expected. What am I doing wrong? Can anyone help me?
Edit:
Hi, Bill Lizard, here are two squares:
Public abstract class A {Protected static list & lt ; String & gt; L; Public A () {} Safe Stable Synchronization List & lt; String & gt; GetList () {if (l == faucet) l = New Linkedist & lt; String & gt; (); Returns L; }} // --- Extends the public class B {Personal Static B Example; Private B () {super (); } Public stable synchronization b getInstance () {if (example == faucet) Example = New B (); Return example; } Public Zero Metodo () {System.out.println (super.getList (). Size ()); . Super.getList () Adding ("A"); Println (. Super.getList () size ()); }}
And thus let me make an example of B:
public class ClassTeste {public static zero main (string [] args) {B} B = Bustest Instance (); B.metodo (); }}
I have run your main revised code with the following main method; < / P>
public static zero main (string [] args) {bb1 = bestsa instance (); B1.metodo (); BB2 = Bust Instance (); B2.metodo (); }
and found the following output, which matches what you searched for
0 1 1 2
Comments
Post a Comment