I had a class like this :
class Test
{
public static test getInstance()
{
return new test()
}
public void firstMethod()
{
//do something
}
public void secondMethod()
{
//do something
}
public void thirdMethod()
{
//do something
}
}
in the another class if we calling Test.getInstance().methodName() several times with different method, what happening?
Which one will be faster and using low memory in following codes?
Test.getInstance().firstMethod()
Test.getInstance().secondMethod()
Test.getInstance().thirdMethod()
or
Test test = Test.getInstance();
test.firstMethod();
test.secondMethod();
test.thirdMethod();
testobject. I think someone will surely put it in his/her answer to explain.getInstance()method, it usually implies that you use the Singleton pattern, which means only the first call will create anew Testand all subsequent calls return that one instance. When you have a factory method which is supposed to return a new instance whenever it is called, call itcreate()ormakeTest(). Also, when the methods of your class don't use any variables, you should make themstaticand call them statically withTest.methodName.Object, which does have to allocate memory. That does take time.