Q: How to make instance one time and use it more times? I should write an application for handling math operations. I have one interface Operator and four classes Plus, Minus, Multiply, Divide). These interface and classes are in separate folders. I should load and use them in my main program. I do it like so:
File operatorFile = new File(operatorPath);
URL operatorFilePath = operatorFile.toURL();          
URL[] operatorFilePaths = new URL[]{operatorFilePath};
ClassLoader operatorsLoader = new URLClassLoader(operatorFilePaths);




Class operatorInterface = operatorsLoader.loadClass("operators.Operator"); 

            FilenameFilter dotClassFilter = new FilenameFilter() 
            { 
                public boolean accept(File dir, String name) 
                { 
                    return name.endsWith(".class"); 
                } 
            };
while(expression != null)
            {
                String[] elementInExpression = expression.split(",");

                if(elementInExpression.length != 3)
                {
                    System.out.println("Expression should have two operand and 1 operator ");
                    throw new ValidationException("Validation on number of element failed1");
                }

                Validation.validateOperand(elementInExpression[0],elementInExpression[1]);
                Validation.validateOperator(elementInExpression[2]);

                double firstNumber = Double.parseDouble(elementInExpression[0]);
                double secondNumber = Double.parseDouble(elementInExpression[1]);

                double output = 0;

                Method methodsInOperator;
                Object instance;
                String operatorSign;

                for(Class operatorCls : operatorClass)
                {
                    instance = operatorCls.newInstance();
                    methodsInOperator = operatorCls.getMethod("getSign", null); 
                    operatorSign = (String)methodsInOperator.invoke(instance, null);
                    if(operatorSign.equals(elementInExpression[2]))
                    {
                        methodsInOperator = operatorCls.getMethod("calculate", new Class[] { double.class, double.class } ); 
                        output =(double)methodsInOperator.invoke(instance, firstNumber, secondNumber);  
                    }
                }

                processingResult.add(output);
                expression = mathExpReader.readLine();
            }

but at the below statement I make an instance for every line I read from file and in this way waste memory. Is there any way to make an instance one time and use it several times?
for(Class operatorCls : operatorClass)
                {
                    instance = operatorCls.newInstance(); //here we waste memory
                    methodsInOperator = operatorCls.getMethod("getSign", null); 
                    operatorSign = (String)methodsInOperator.invoke(instance, null);
                    if(operatorSign.equals(elementInExpression[2]))
                    {
                        methodsInOperator = operatorCls.getMethod("calculate", new Class[] { double.class, double.class } ); 
                        output =(double)methodsInOperator.invoke(instance, firstNumber, secondNumber);  
                    }
                }


A: Create class using singleton pattern then you have to initialize object once and you can use it anywhere in project.
here is tutorial of singleton pattern. 
