File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes What is this code doing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is this code doing" Watch "What is this code doing" New topic

What is this code doing

Hugh Harrison

Joined: Mar 01, 2010
Posts: 3
I am studying a JSP book(JSP: Complete Reference by Hanna published by Osbourne) that includes sample code for a java class that one can use instead of a browser to display the server response to a request. The class name is WebClient. It has a main() method. It also has a constructor - public WebClient(String host, int port). The "main" method contains the following statement: new WebClient(host, port). Is this statement calling the constructor OR instantiating an inner class of the same name as the outer class OR what? If it is merely creating a new instance of the WebClient class, why would that be necessary? When one starts a java program with a mian method doesn't the constructor execute.

Shinelin Samuel
Ranch Hand

Joined: Mar 01, 2010
Posts: 55

Going with your statements, it would call the constructor WebClient(host, port) and creates a class instance. The constructor will be called only during instantiation of the class.

David Newton

Joined: Sep 29, 2008
Posts: 12617

No, simply running main doesn't create an instance of the class it's in--main is a static method. If you want the main method to create an instance of the class you have to "new" it.

And welcome to JavaRanch!
I agree. Here's the link:
subject: What is this code doing
It's not a secret anymore!