Does Parameter pass to the method in the same way as object creation?

I study servlets and JSP. I'm curious about doGet and other methods that can be overestimated. "DoGet" takes 2 parameters - an HTTPServletRequest request and an HTTPServletResponse response. This is my question: request and response objects are used inside the method body, but I do not see any object creation, for example. request = new HTTPServletRequest. These objects are created elsewhere, for example. in the superclass? This is just a Java question, since I often ask this question using applets, i.e. A Graphics g object is passed to the paint method, but I don’t see it being created anywhere?

Gf

+4
source share
5 answers

In the two examples you provided, servlets and applets, the code runs inside the container. Tomcat is a container for servlets, which means that the container provides certain functions. In this case, the container will create request and response objects and give you your servlet.

If you are writing a simple Java program that runs on its own, you are responsible for creating all the objects.

+2
source

As a rule, in any programming language, when a method is called with object instances (or any parameter, for that matter), yes, these objects are created somewhere.

For the most part, you don’t have to worry about where they are when they deal with them within your functions.

Returning to your question, although there may be certain situations when an object was created using unconventional means (depending on the technological stack), you can be sure that most often, if you have a link to the object passed to you in the method that you wrote, then it was created using traditonal methods somewhere in the call stack (or another if you have multiple threads).

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

+2
source

It is created by a web server (e.g. tomcat) and it calls your servlet with these parameters

+1
source

Objects are created on the call site. That is, the one who names the method is responsible for creating the objects that he passed to the method as parameters (if he does not pass existing objects, of course, but those that were previously created somewhere else).

0
source

all methods in servlets are called by a servlet container like tomcat

0
source

Source: https://habr.com/ru/post/1301381/


All Articles