开发者

Is Passing a Parameter to a method the same as creating an object?

开发者 https://www.devze.com 2022-12-20 11:24 出处:网络
I am learning Servlets and JSP. I am wondering about the \"doGet\" and other methods that may be overidden. The \"doGet\" takes 2 params - HTTPServletRequest request, and HTTPServletResponse response.

I am learning Servlets and JSP. I am wondering about the "doGet" and other methods that may be overidden. The "doGet" takes 2 params - HTTPServletRequest request, and HTTPServletResponse response. This is my question: The request and response objects are used within the method body but I do not see any object creation e.g. request = new HTTPServletRequest. Are these objects开发者_如何学编程 created elsewhere e.g. in the superclass? This is just a Java question really as I often wonder about this with Applets also, i.e. the Graphics g object is passed to the "paint" method but I don't see it's creation anywhere?

GF


In the two examples you gave, servlets and applets, the code is running inside of a container. Tomcat is the container for servlets and that means that the container provides certain functionality. In this case the container will create the request and response objects and pass it to your servlet for you.

If you write a plain Java program that runs by itself, then you are responsible for creating all objects.


Generally, in any programming language, when a method is called with instances of an object (or any parameter for that matter), yes, those objects are created somewhere.

For the most part, you don't have to worry about the where, just that they are when dealing with them inside of your functions.

Getting back to your question though, while there might be certain situations that an object was created through non-traditional means (depending on the technology stack), you can be assured that more often than not, if you have a reference to an object passed to you in a method you wrote, then it was created using traditonal means somewhere in the call stack (or another, if you have multiple threads).

In the case of Java, this would mean that someone called new ... at some point, and made it available to the call site of your method in order to pass it in as a parameter.


It's created by the web server (tomcat, for example), and it calling your servlet with this parameters


The objects are created at the call-site. I.e. whoever calls the method is responsible for creating the objects that he/she passed to the method as parameters (unless he passes already existing objects, of course, but those have previously been created somewhere as well).


all methods in servlets are invoked by servlet container such as tomcat

0

精彩评论

暂无评论...
验证码 换一张
取 消