-
Static methods question
Hi Everybody!
I am going through the Java 2 Advanced Features book by sun and have what
I think is a very simple question regarding static methods.
A static method is basically one that you do not need to create an instance
of the parent class to call that method. Here is a class in an example from
the book:
class SocketOpener implements Runnable
{
public static Socket openSocket(String aHost, int aPort, int timeout)
{
Thread t = new Thread(opener);
t.start();
try
{
t.join(timeout)
}
catch (InterruptedException e)
{
}
return opener.getSocket();
}
public SocketOpener(String aHost, int aPort)
{
socket = null;
host = aHost;
port = aPort;
}
public void run()
{
try
{
socket = new Socket(host,port);
}
catch ( e)
{
}
}
public Socket getSocket()
{
return socket;
}
private String host;
private int port;
private Socket socket;
}
So the openSocket method which is declared as static can be invoked from
another class simply by using:
Socket s = SocketOpener.openSocket(host,port,timeout);
What I don't understand is how does the SocketOpener class use the host and
port arguments passed to the openSocket method? Does calling a static method
cause the classes constructor to be called automatically and the constructor
automatically gets the arguments passed to the static method?
This sounds like it should be easy and unfortunately I left my fundamentals
book at home today. Any help would be greatly appreciated!
Thanks!
David Rancour
-
Re: Static methods question
OK, I found my problem...I missed a line of code which called the constructor
from the openSocket Method. This fixes things for me. Sorry for the unnecessary
post!
"David Rancour" <david_rancour@hotmail.com> wrote:
>
>Hi Everybody!
>
>I am going through the Java 2 Advanced Features book by sun and have what
>I think is a very simple question regarding static methods.
>
>A static method is basically one that you do not need to create an instance
>of the parent class to call that method. Here is a class in an example from
>the book:
>
>class SocketOpener implements Runnable
>{
> public static Socket openSocket(String aHost, int aPort, int timeout)
> {
> Thread t = new Thread(opener);
> t.start();
> try
> {
> t.join(timeout)
> }
> catch (InterruptedException e)
> {
> }
> return opener.getSocket();
> }
>
> public SocketOpener(String aHost, int aPort)
> {
> socket = null;
> host = aHost;
> port = aPort;
> }
>
> public void run()
> {
> try
> {
> socket = new Socket(host,port);
> }
> catch ( e)
> {
>
> }
> }
>
> public Socket getSocket()
> {
> return socket;
> }
>
> private String host;
> private int port;
> private Socket socket;
>}
>
>So the openSocket method which is declared as static can be invoked from
>another class simply by using:
>
>Socket s = SocketOpener.openSocket(host,port,timeout);
>
>What I don't understand is how does the SocketOpener class use the host
and
>port arguments passed to the openSocket method? Does calling a static method
>cause the classes constructor to be called automatically and the constructor
>automatically gets the arguments passed to the static method?
>
>This sounds like it should be easy and unfortunately I left my fundamentals
>book at home today. Any help would be greatly appreciated!
>
>Thanks!
>
>David Rancour
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks