Ondasboy Ondasboy - 6 months ago 41
Java Question

non-static variable this cannot be referenced from a static context issue when instantiating a new object

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);

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 :)


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).