LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   reducing no: of objects created, to improve performance... (https://www.linuxquestions.org/questions/programming-9/reducing-no-of-objects-created-to-improve-performance-474328/)

sachitha 08-16-2006 04:05 AM

reducing no: of objects created, to improve performance...
 
with the original code being the following, can i reduce the no: of times that objects(GifFileSelector objects..) are created in the method 'open()' by using the coding that i have put below???
The objects are not created within a for loop continuously.. so i'm not sure if i can make any change to 'reuse objects'.. (provided this open() method is called several times)

original :
Code:

public class FileMenu extends AbstractMenu
{
        public final int INDEX = 0;
        private static final String TITLE = "File";
        private static final int MNEMONIC = KeyEvent.VK_F;

..........
public void itemSelected(String itemName)
        {
                if (itemName.equals(MENU_ITEMS[0].getName()))
                        open();
                    .......
                }



private void open()
        {
  GifFileSelector g = new GifFileSelector();

            .........
              }

The changes that i made:
Code:

public class FileMenu extends AbstractMenu
{
        public final int INDEX = 0;
        private static final String TITLE = "File";
        private static final int MNEMONIC = KeyEvent.VK_F;
        GifFileSelector g;  //creating GifFileSelector instance

...............

private void open()
        {
       
                if (g == NULL)
                {
                        g = new GifFileSelector();
                }
                int selected = g.showOpenDialog(MDIFrame.getDesktop());
                if (selected == JFileChooser.APPROVE_OPTION)
                {
                        try
                        {

                                                       
                        .........       
                        }
                              }
                        }


xhi 08-16-2006 07:49 AM

yes, the code you used is what you would typically find in a getter method (beans anyone).

Code:

public GifFileSelector getFileSelector(){
    if (g == null){
        g = new GifFileSelector();
    }
    return g;
}


sachitha 08-17-2006 02:59 AM

so does that mean, that i 'm not making any difference(performance improvement) at all to the code, provided the open method is called more than once???
i mean if the open method is called (more than one picture is being opened at different times) am i not reducing the no: of objects being created ..?

xhi 08-17-2006 08:48 AM

Quote:

Originally Posted by sachitha
so does that mean, that i 'm not making any difference(performance improvement) at all to the code, provided the open method is called more than once???
i mean if the open method is called (more than one picture is being opened at different times) am i not reducing the no: of objects being created ..?

i said yes in reponse to
Quote:

with the original code being the following, can i reduce the no: of times that objects(GifFileSelector objects..) are created in the method 'open()' by using the coding that i have put below???

sachitha 08-17-2006 09:46 PM

okay.. thanks! :)
well need a lil clarification... If this particular open() method is called only 'once' , both the original coding and my code will do the same thing , ya?
Since, in the modified code that i put, 'g' will be equal to NULL, the object will be created for the first time ya?

xhi 08-17-2006 10:27 PM

you are correct


All times are GMT -5. The time now is 02:15 PM.