2

I have few questions about efficient memory usage. I have a form which will be submitted to receive a list of items as result. Once form is submitted its fields will be received by request variable and will be processed by submitRequest method of model class.

Question is that where should I allocate memory to request variable (Request request = new Request) ? in constructor? or do not allocate it at all as it works now?

Second question is that where should I make an object of Model class as it is being used by different methods. Should I define it in each method or it is better to have a single object of it?

public class MyClass{

  private Request request;
  private List<result> results;
//private formModel myModel = new formModel();

  public MyClass(){
  }

  public String myForm(){
   formModel myModel = new formModel(); 
   this.results = myModel.submitRequest(request); //process request and return results
   return "SUCCESS";
  }
  .......
}
Roman C
  • 49,761
  • 33
  • 66
  • 176

2 Answers2

0

where should I allocate memory to request variable (Request request = new Request) ? in constructor? or do not allocate it at all as it works now?

Actually that should not be a problem, since Java cares about all the memory stuff. It would be possible to initialize the variables when you declare them. In this case it is not possible to get a NullPointerException because the variables should be already initialized.

Of course, when you do it this way you initialize all variables when the object is created. But again, this should not be a performance issue.

where should I make an object of Model class as it is being used by different methods. Should I define it in each method or it is better to have a single object of it?

Regarding the model instance, it depends. If you want to share one object over multiple methods it is a good idea to create a private class variable.

Otherwise, if you only want to use the model instance inside one of your methods then you should only create the variable inside that method.

Roman C
  • 49,761
  • 33
  • 66
  • 176
mvieghofer
  • 2,846
  • 4
  • 22
  • 51
0

where should I allocate memory to request variable (Request request = new Request) ? in constructor? or do not allocate it at all as it works now?

You might do it anywhere in your code, in the constructor, inline, lazy, via container, using other DI frameworks, using Struts type conversion, interceptors, etc. What you should follow is a Java Bean convention.

where should I make an object of Model class as it is being used by different methods. Should I define it in each method or it is better to have a single object of it?

The second question is addressed to threadsafety of action class objects. In the previous major version of Struts the user beans are passed as parameters to the action methods to be threadsafe because action classes were not.

In the current version of Struts the action classes are threadsafe because the new instance of the action class is created per action invocation. And you can create a property for the Model in the action class that could be used by different methods. In this case case you could define the scope of the object being used.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    Thanks, I do not get what you mean by "unless they are not different actions" what do you mean by "should define the scope of the object being used" formModel is a class which has methods to access database as this need will be used by most of methods should I have it as "private formModel myModel = new formModel();"? or define it in each class? , and results is used by JSP to show the results, I put it as a field to be accessible by JSP. sorry my english is quite weak. –  Nov 27 '13 at 20:53
  • in the simple case yes, initialize inline. The scope will be `default` as the action scope. More about scopes available in struts container see the bean configuration. And [this](http://stackoverflow.com/questions/17244036/dependency-injection-in-struts2-accessing-session-scoped-beans) question might help you. – Roman C Nov 27 '13 at 21:03