Search code examples
javanio

Cannot instantiate the type SocketChannel


According to the docs, I can do this:

SocketChannel mySocketChannel = new SocketChannel(SelectorProvider.provider());

According to JDK I can not.

Simple question: Why am I getting the error in the title?


Solution

  • The constructor is protected, meaning only inherited classes can access it to inherit its base functionality (notice that SocketChannel inherits from AbstractSelectableChannel).

    To open a SocketChannel, use the static open method:

    SocketChannel socketChannel = SocketChannel.open();
    

    See the SocketChannel Documentation and an example.