Java Instance return model note!
2013-02-17 09:49
323 查看
1. We have a class and define a static method of getInstance()
public class GenDarwinGlobal extends GenDarwinGlobalHelper
{
/**
* Script Name : <b>GenDarwinGlobal</b>
* Generated : <b>Mar 31, 2011 1:16:48 AM</b>
* Description : Functional Test Script
* Original Host : WinNT Version 5.2 Build 3790 (S)
*
* @since 2011/03/31
* @author dwei
*/
public void testMain(Object[] args)
{
// TODO Insert code here
}
public static GenDarwinGlobal getInstance()
{
return new GenDarwinGlobal();
}
}
2. Compare these code line, the getInstance sequence will get failed return because it has two new instance belong to different object.
// FucLib.GenevaFuc.GenDarwinGlobal.getInstance().openBuyTransaction();
// FucLib.GenevaFuc.GenDarwinGlobal.getInstance().ClickQuery();
FucLib.GenevaFuc.GenDarwinGlobal oGD = new FucLib.GenevaFuc.GenDarwinGlobal();
oGD.openBuyTransaction();
oGD.ClickQuery();
3. Also we can using this method to resolve this problem in the instance code .
private static GenDarwinGlobal instance = null;
public static synchronized GenDarwinGlobal getInstance()
{
if( instance == null)
{
instance = new GenDarwinGlobal();
}
return instance;
}
public class GenDarwinGlobal extends GenDarwinGlobalHelper
{
/**
* Script Name : <b>GenDarwinGlobal</b>
* Generated : <b>Mar 31, 2011 1:16:48 AM</b>
* Description : Functional Test Script
* Original Host : WinNT Version 5.2 Build 3790 (S)
*
* @since 2011/03/31
* @author dwei
*/
public void testMain(Object[] args)
{
// TODO Insert code here
}
public static GenDarwinGlobal getInstance()
{
return new GenDarwinGlobal();
}
}
2. Compare these code line, the getInstance sequence will get failed return because it has two new instance belong to different object.
// FucLib.GenevaFuc.GenDarwinGlobal.getInstance().openBuyTransaction();
// FucLib.GenevaFuc.GenDarwinGlobal.getInstance().ClickQuery();
FucLib.GenevaFuc.GenDarwinGlobal oGD = new FucLib.GenevaFuc.GenDarwinGlobal();
oGD.openBuyTransaction();
oGD.ClickQuery();
3. Also we can using this method to resolve this problem in the instance code .
private static GenDarwinGlobal instance = null;
public static synchronized GenDarwinGlobal getInstance()
{
if( instance == null)
{
instance = new GenDarwinGlobal();
}
return instance;
}
相关文章推荐
- Java Instance return model note!
- Java Instance return model note!
- Java method Exception throw with return instance.
- Java method Exception throw with return instance.
- java中void方法return的用法
- java.lang.IllegalArgumentException: No converter found for return value of type
- GENERIC FRAMEWORK MODEL OF JAVA PLATFORM
- Java中的return语句使用总结 比较javascript的return
- Java中的return语句使用总结
- Java中break return continue 区别
- java中的return 语句和finally的执行顺序关系
- Java finally语句到底是在return之前还是之后执行?
- RoyalAjax(9)------AjaxReturnBean.java
- Java中关键字continue、break和return的区别
- java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.Has
- [学习笔记]Thinking in Java (the 2nd edition) Study Note (4)
- 关于java中的try-catch-finally语句和return
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- java-memory-model
- java编译错误处理No enclosing instance of type demo1_1 is accessible.