6

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();
    } 
}
2
  • 3
    The method listen() is undefined for the type Object It's pretty straight forward. The Object class does not contain that method. Declare your EchoServer0 variable as a type with that method.
    – takendarkk
    Jan 20, 2014 at 23:52
  • The compiler uses the declared type of a variable to see what methods it supports. If you write Object name="Stack Overflow", then you can call on name only the methods of Object. The compiler has no choice. Between the declaration and a method call, somebody could write name = Integer.valueOf(1). The actual method that gets called depends on the value of name, and the declared types of the method arguments. Jan 22, 2014 at 6:36

4 Answers 4

13

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.
5
  • 2
    You might want to give a snippet of info about why this works compared to what the OP had instead of just the code. +1 for correct solution though.
    – takendarkk
    Jan 21, 2014 at 0:00
  • Thanks. I will add a few comments.
    – Raul Guiu
    Jan 21, 2014 at 0:12
  • 1
    This isn't "only" a naming convention problem, because the variable name has the same name as his class name, which can only cause confusion.
    – ajb
    Jan 21, 2014 at 0:37
  • I had to try to see if that compiles.
    – Raul Guiu
    Jan 21, 2014 at 1:05
  • EchoServer EchoServer = new EchoServer(); EchoServer.echo(); // and echo() is not static!!
    – Raul Guiu
    Jan 21, 2014 at 1:08
1

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.

2
  • This is just pure curiosity: Why don't you declare and initialize on the same line? Just preference?
    – takendarkk
    Jan 20, 2014 at 23:54
  • Yeah. He/she might want to do other stuff in between, I guess. But this is really just a habit more than anything. Jan 20, 2014 at 23:55
1

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

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.