Results 1 to 2 of 2
  1. #1
    linking90 is offline Member
    Join Date
    Jul 2010
    Rep Power

    Default Creating Java objects using "new"

    In the following method, while creating Java servlets one of the methods used (according to the book Im refering too)
    doGet(HttpServletRequest req, HttpServletResponse res)
    req is an object of type HttpServletRequest but one does not need to write
    HttpServletRequest req = new HttpServletRequest

    Even in the following method, there is no mention of new for creating PrintWriter object
    PrintWriter Systemout = res.getWriter();
    Im new to java and trying to understand when to use "new" AND when not to.
    Thank you for your responses.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    Its possible for methods to return objects. The res.getWriter() method probably does that.
    Read the API doc the class and method for further information.

    You use new when YOU want to create an object. In the method declaration:
    doGet(HttpServletRequest req, HttpServletResponse res)
    req and res are objects passed to that method. They were created previously.

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 0
    Last Post: 11-22-2008, 01:49 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts