-1

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?

Note: I know what static and non-static methods are. I just don't know why this non-static method is behaving as if it's static.

  • 2
    `getSoTimeout()` is operated on an object, not a class! – Am_I_Helpful May 20 '17 at 15:47
  • @Am_I_Helpful I'm sorry, I don't understand what this means. How would I implement it? – Finder Bear May 20 '17 at 16:00
  • 1
    @FinderBear This is about the **Basics** of OO-Programming, What is an `Object`, what is a `Class`, What is the difference between `Static` and `Non-Static` Method ..etc. – Yahya May 20 '17 at 16:07
  • Not a duplicate. My issue was that I didn't understand the difference between a function for a class and an object – Finder Bear May 21 '17 at 03:05
  • There is nothing in the error message about asking for a static method. You need to read what the message actually says. Not what you think it says. And you need to report it here accurately. – user207421 May 21 '17 at 03:32
  • @EJP I have copy-pasted the error message here as displayed. I also never said anything about it asking for a static method. Quite the opposite in fact. Anyway, this question already has a solution- no need to continue to nitpick about it. – Finder Bear May 21 '17 at 04:25
  • You have said **exactly** that in the title of your question. – user207421 May 21 '17 at 12:22

1 Answers1

0

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) {
    }
}
Blaine
  • 332
  • 1
  • 4
  • 18