aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""public static void main(String[] args)" rule?" Watch ""public static void main(String[] args)" rule?" New topic
Author

"public static void main(String[] args)" rule?

Ben Johnson
Greenhorn

Joined: Jul 12, 2005
Posts: 17
Hi folks,

I'm reading "Core Java 2 - Vol. 1 - Fundamentals" by Cay Horstmann and Gary Cornell, and, in the middle of page 37, they say:

"The point to remember for now is that every Java application must have a main method that is declared in the following way:



Is this to be taken literally? EVERY program must contain this this method declared? If it's useful information, I'm writing Beans/Servlets for the Web, not standalone Java apps. I have written some Beans that don't have a main method and they work fine. Is there some larger reason to do this that I'm overlooking? I don't even understand what exactly this method does. Might someone please explain to me if this is necessary for all applications, and if so, what its purpose is?

This may or may not be related, but in my class files I have a method right after the class declaration that has the exact same name (I learned this by following examples in books) in which I declare default values for variables and such; is that method called upon invoking the servlet/bean, because they have the same name? It seems to work that way.

Thanks so much for your time!

Ben
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
The main method is used by stand alone apps as an entry point. Applets do not need them. In your case using Servlets, I don't think you need one. I believe you will use one of the methods (doPost - doGet)in HttpServlet to kick things off. Your Beans certainly won't need one. In a stand alone app you only need one main method as a point for the application to start, but the application could contain numerous other classes that don't have a main method.

This may or may not be related, but in my class files I have a method right after the class declaration that has the exact same name (I learned this by following examples in books) in which I declare default values for variables and such; is that method called upon invoking the servlet/bean, because they have the same name? It seems to work that way.


This is called a constructor.

Hope this helps!
[ September 28, 2005: Message edited by: Hentay Duke ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"...every Java application..." (i.e., stand-alone program).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ben Johnson
Greenhorn

Joined: Jul 12, 2005
Posts: 17
Okay, great, thank you both for clearing that up for me. Yeah, it's a little hard because the book I have isn't specifically tailored for Web applications - it's just a Java-in-general kind of thing, although, it has been pretty useful in explaining most concepts.

The one thing I still don't really feel like I understand, however, is the difference between a Bean and a Servlet. Near as I can tell, they are the same; just a class with methods. In the OOP/MVC reading (and re-reading...and re-reading) I did, I gathered that they are in fact quite similar by nature, but it's their intended uses that differ. I thought they explained that the servlet is basically a way of handling/managing requests as they come in and doing things like redirect, supply XML instead of HTML if the user agent requests XML, force authentication, etc. By comparison, the code in a Bean looks pretty much the same, minus the obligatory doGet/doPost methods, and executes a more specific set of instructions.

Am I way off the mark here?

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See this page for a good overview of a JavaBean...

http://java.sun.com/developer/onlineTraining/Beans/bean01/page2.html

Or this page for a broader view of Beans...

http://java.sun.com/developer/onlineTraining/Beans/
[ September 28, 2005: Message edited by: marc weber ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You execute your servlet under a servlet container. That servlet container has got a main method. The same can be said for EJB containers. The term 'application' has been misused by your book, since obviously a servlet certainly qualifies as a 'Java application'.


Tony Morris
Java Q&A (FAQ, Trivia)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

> The one thing I still don't really feel like I understand, however, is the difference between a Bean and a Servlet. Near as I can tell, they are the same; just a class with methods.

Java beans and servlets are both just classes with methods, but beans and servlets are not at all the same thing.

A Java bean is just a class with member variables and get...() and set...() methods to get and set the values of those member variables.

A servlet is a component that extends class javax.servlet.http.HttpServlet and that implements methods like doGet(), doPost() etc. (see the API documentation of class HttpServlet). You run servlets inside a servlet container like Tomcat. Servlets have the same function as CGI or PHP scripts - they are run by the servlet container when a client (using a web browser) requests a specific URL, and they generate a HTML page which is sent back to the client.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
If you want to concentrate on web apps. A good book to get would be the "Head First Servlets & JSPs".
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

You can't run a servlet by itself, so it doesn't technically qualify as an application. I think it would be more correct to say that a servlet is an extension, a way to extend the behavior, of a Java application, which is the servlet container.


SCJP, SCWCD
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Edwin Keeton:
You can't run a servlet by itself, so it doesn't technically qualify as an application. I think it would be more correct to say that a servlet is an extension, a way to extend the behavior, of a Java application, which is the servlet container.

I have to agree. Just as an applet requires a browser (or viewer), a servlet requires a container. It is not a stand-alone program.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Hentay Duke:
If you want to concentrate on web apps. A good book to get would be the "Head First Servlets & JSPs".

Yes, this is a very popular one, but its style is quite different than than Sun's "Core" books. If you like Horstmann and Cornell's Core Java, then you might consider Core Servlets and JavaServer Pages, Vol. 1: Core Technologies (2nd ed) by Marty Hall and Larry Brown. Samples -- including complete text of the 1st edition -- can be found at coreservlets.com. (Thanks to Ben Souther for recommending this book to me on the Apache/Tomcat board .)
[ September 29, 2005: Message edited by: marc weber ]
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Certainly pick which ever book you like the best. The point I would make is that reading "Core Java" and writing beans and servlets probably isn't the best combination. Either you have a good grasp of the core API and need a book more specialized, or you're just starting out and shouldn't worry about beans and servlets quite yet.

Just trying to help make it easier for you.
Naveen Vooka
Greenhorn

Joined: Oct 13, 2004
Posts: 25
If you want to work with Servlets and JSP have a look at this link
http://www.murach.com/downloads/jsps.htm

--------------
Naveen Vooka
www.devsquare.com
DevSquare - Online Application Development
Timothy Toe
Ranch Hand

Joined: Oct 19, 2002
Posts: 156
Ironically, sometimes, simple questions are hard to answer. This is one fine example.

I think
Tony Morris' post and
Edwin Keeton's post
are good posts/replies.


Timothy
---
SCJP, SCBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "public static void main(String[] args)" rule?