Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is this code doing

 
Hugh Harrison
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

THANKS
 
Shinelin Samuel
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Regards,
Shinelin
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic