Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

saveErrors method in Action class

 
jose chiramal
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following in my code:

ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.logon.connect"));

saveErrors(request,errors); // how am i able to call the saveErrors method without a object ?
return (new ActionForward(mapping.getInput()));
}
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean, without an object?
 
jose chiramal
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saveErrors() is a method of org.apache.struts.Action class. So when we call saveErrors() why are we not using object.methodname() ?

Can i say that the ActionServlet takes care of creating the instance of Actionform ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:saveErrors() is a method of org.apache.struts.Action class. So when we call saveErrors() why are we not using object.methodname() ?

Because you're in the action whose saveErrors() you're calling... you might want to brush up on some Java basics before proceeding much further--if you're getting lost with casting and Java programming fundamentals it's a lot more productive to get them nailed down before diving into applications.
Can i say that the ActionServlet takes care of creating the instance of Actionform ?

The request processor instantiates the action forms.
 
jose chiramal
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David Newton,

In java any method has to be called using an object unless it is a static method which can be called directly or using class name. In this case saveErrors() is not static, how are we able to call saveErrors(request,errors) directly without using any object is my question.

Awaiting a reply...
 
jose chiramal
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supporting my previous post , for example to use the add method in ActionErrors i use :

ActionErrors errors = new ActionErrors();

errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.general");
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:In java any method has to be called using an object unless it is a static method which can be called directly or using class name. In this case saveErrors() is not static, how are we able to call saveErrors(request,errors) directly without using any object is my question.


Java 101. Please, please go back to the basics before considering doing much else, otherwise almost all code is going to look very confusing to you.

See Foo.baz()? It calls bar().

bar() is an instance method of class Foo that can be called by "any" other (non-static) method of Foo.

A Struts 1 action is an instance of class Action. Your action is a subclass of Action. Your action methods can call other methods of your action--or methods contained in its superclass... which is Action.

There's an implicit this in front of instance method calls within an instance. saveErrors(...) is an instance method of an Action. Essentially every single non-trivial Java class will call instance methods like this--it's almost impossible you haven't seen this before.
 
jose chiramal
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i remember having seen this ..thanks David Newton for your reply.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic