Finder Bear Finder Bear -4 years ago 63
Java Question

Socket.getSoTimeout always asks for static method

I have the following code

import java.net.Socket;
import javax.swing.JOptionPane;

public class SettingTimeout {

public SettingTimeout() {
Socket.getSoTimeout();

}

public static void main(String[] args) {
}
}


The problem is that it will not compile, giving the error:


Cannot make a static reference to the non-static method getSoTimeout() from the type Socket


However, the constructor is obviously not static. What am I doing wrong?

Answer Source

You must first create an object. the getSoTimeout method is specific to a certain socket opening, and not a global value, therefore, you need to first open a socket, meaning your code will look something like this:

import java.net.Socket;
import javax.swing.JOptionPane;

public class SettingTimeout {

    public SettingTimeout() {
        Socket s = new Socket();            
        s.getSoTimeout();

    }

    public static void main(String[] args) {
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download