• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

making/calling a package

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package com.javaranch; // does it making a package or calling a package ? but calling needs // import.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PostServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>\n <body>\n");
out.println(" <h2>\n Hello World\n </h2>\n");
out.println(" </body>\n</html>\n");
out.flush();
out.close();
}
}
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Salvador -

The package declaration indicates the package location of your class. It does not execute a call to the package (in fact, I don't believe packages are executable). In your example, you do not need to import the package com.javaranch.

--Leo
 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package com.javaranch; // it is declaring only ?

import java.io.*; // it is calling only ?

wwhat is the difference ?
 
Leo Deegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The package line says that your PostServlet class is in the com.javaranch package, which means it is in the subdirectory com/javaranch relative to your source root directory.

If you have the import java.io.* line, you are indicating that your class can use the java.io package classes without fully qualifying their names. If you notice, your class can use a PrintWriter without having to call it java.io.PrintWriter each time you use the PrintWriter name.

When you declare a package, you are not calling the package. When you import the set of classes in a package (e.g., import java.io.*), you are also not making a call.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The package statement allows you to group several classes and interfaces together into a "package". You can then reference the classes you created in that package in other java programs by importing the package.

Example:

package com.myNewPackage;

import com.javaranch.*;

public class myNewClass
{
PostServlet ps = new PostServlet();

// more code...
}
 
I can't take it! You are too smart for me! Here is the tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic