java:7: error: invalid method declaration; Return type required

Asked

Viewed 279 times

-1

Name of the complete error:

Cliente.java:7: error: invalid method declaration; return type required
  public cliente() {
         ^

Code I used:

import java.net.Socket;


public class Cliente extends javax.swing.JFrame {
     private static Socket cliente;

public teste() {
    initComponents();

}

public static void main(String[] args){


}
private static void iniciaCliente(){
    try{
        cliente = new Socket("127.0.0.1 ",4444);
        System.out.println("Conexão feita com sucesso");
    } catch (IOException ex){

        System.out.println("ErroConnect:");

        }

    }

}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

1

The error itself is exactly what is described by the compiler, speaks the type of return of the method. If there is no return then the type is void, thus:

public cliente() {

I put in the Github for future reference.

But every method that returns void tends to be misguided, so think carefully if you should have this method like this.

If you return something then the type must be exactly of the returning object. Java is a static, nominal, and virtually all explicit typing language, so you have to say the kind of things.

But it has a method that has no type because it should always be the type of class you are declaring. This is the method builder. If this is the case, and it appears to be, then your error is just typing, and wrote a method cliente() (c lower) when it should be Cliente() (Uppercase C), so if the method does not have the same class name the compiler thinks it is not the constructor and indicates an error, its error caused a misinterpretation in the compiler.

  • now it is giving the error "Client.java:8: error: cannot find Symbol"" "Initcomponents" I will put in the question edition

  • 1

    I imagine it will be quite a mistake, you have to understand what you’re doing, if playing random things can happen at all, I’m almost sure it’s typo again. You cannot keep editing the question to ask new questions. Read [tour] and [help] how the site works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.