-1
import java.io.*;
import java.net.*;

public class pwundublfive{
    public class Server{

        private ServerSocket socket;
        private int port;

        public Server(int port) throws IOException{
            socket = new ServerSocket(port);
            socket.setSoTimeout(10000);
        }
    }

    public static void main(String[] args){
        int port = 1234;
        Server obj = new Server(port); // <-- Error here
    }

}

I know this question has been posted numerous times, and I understand most of the answers that well you can't call a member method on a class. But I'm instantiating a new object, which calls its constructor?? Thanks :)

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
Tetszik7
  • 13
  • 2

2 Answers2

1

The whole class Server is not static, so cannot be referenced in a static method.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
1

The problem is that Server is a non-static inner class of pwundublfive, so you can either instanciate pwundublfive and than call pwundublfiveInstance.Server(port) or (which you should prefer) you can move your Server definition out of the other class into a separate file (called Server.java).

C. L.
  • 571
  • 1
  • 8
  • 26