[java] Why is my method undefined for the type object?

I'm not sure why Eclipse is giving me this error:

The method listen() is undefined for the type Object

What simple mistake am I making? Also, is my code the right way to write a main method which instantiates an EchoServer0 object and calls its listen method?

public class EchoServer0 {    
    public void listen() {
        ServerSocket socket = null;
        try{
            socket = new ServerSocket(2013);
            System.out.println("Opened server socket");
            socket.setSoTimeout(2000);
            socket.accept();
            socket.close();
        }
        catch (SocketTimeoutException ste){
            System.out.println("Timed out after " + 2000 + " ms");
        }
        catch (Exception e){
            System.out.println(e.getClass().getName()+" at server: " + e.getMessage());
        }       
    }

    public static void main(String[] args) {
        Object EchoServer0;
        EchoServer0.listen();
    } 
}

This question is related to java sockets

The answer is


The line

Object EchoServer0;

says that you are allocating an Object named EchoServer0. This has nothing to do with the class EchoServer0. Furthermore, the object is not initialized, so EchoServer0 is null. Classes and identifiers have separate namespaces. This will actually compile:

String String = "abc";  // My use of String String was deliberate.

Please keep to the Java naming standards: classes begin with a capital letter, identifiers begin with a small letter, constants and enums are all-capitals.

public final String ME = "Eric Jablow";
public final double GAMMA = 0.5772;
public enum Color { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET}
public COLOR background = Color.RED;

Try this.

public static void main(String[] args) {
    EchoServer0 myServer;
    myServer = new EchoServer0();
    myServer.listen();
}

What you were trying to do was declaring a variable of type Object, not creating anything for that variable to reference, then trying to call a method that didn't exist (in the class Object) on an object that hadn't been created. It was never going to work.


Change your main to:

public static void main(String[] args) {
    EchoServer echoServer = new EchoServer();
    echoServer.listen();
}

When you declare Object EchoServer0; you have a few mistakes.

  1. EchoServer0 is of type Object, therefore it doesn't have the method listen().
  2. You will also need to create an instance of it with new.
  3. Another problem, this is only regarding naming conventions, you should call your variables starting by lower case letters, echoServer0 instead of EchoServer0. Uppercase names are usually for class names.
  4. You should not create a variable with the same name as its class. It is confusing.

It should be like that

public static void main(String[] args) {
        EchoServer0 e = new EchoServer0();
        // TODO Auto-generated method stub
        e.listen();
}

Your variable of type Object truly doesn't have such a method, but the type EchoServer0 you define above certainly has.