Static methods question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Static methods question

  1. #1
    David Rancour Guest

    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

  2. #2
    David Rancour Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center