java - invalid method declaration; return type req

2019-01-27 07:10发布

问题:

This question already has an answer here:

  • Java error - “invalid method declaration; return type required” 4 answers

On a Java OOP project I got three errors on my constructor:

.\Voter.java:14: error: invalid method declaration; return type required

.\Candidates.java:7: error: invalid method declaration; return type required

.\Candidates.java:14: error: invalid method declaration; return type required

codes for constructor:

public class Voter{
    private String name;
    private int votNum;
    private int precint;

    public Voter(String name, int votNum, int precint)
    {
        this.name = name;
        this.votNum = votNum;
        this.precint = precint;
    }

    public setDetails(String name, int votNum, int precint)
    {
        this.name = name;
        this.votNum = votNum;
        this.precint = precint;
    }...}



public class Candidates
{
    public String candName;
    private int position;
    private int totalVotes;

    public Candidate (String candName, int position, int totalVotes)
    {
        this.candName = candName;
        this.position = position;
        this.totalVotes = totalVotes;
    }

    public setDetails (String candName, int position, int totalVotes)
    {
        this.candName = candName;
        this.position = position;
        this.totalVotes = totalVotes;
    }...}

i declared my constructors like this:

public class MainClass{
    public static void main(String[] args){
        System.out.println("Previous voter's info: ");
        Voter vot1 = new Voter("voter name", 131, 1);
        System.out.println("The Candidates: ");
        Candidates cand1 = new Candidates("candidate name", 1, 93);
    }
}

Anything I missed?

回答1:

In your method setDetails you haven't specified anything for the return type, if it is not returning anything then specify void

For Voter class

public void setDetails(String name, int votNum, int precint)

for Candidates class

public void setDetails (String candName, int position, int totalVotes)

One other thing, (Thanks to Frank Pavageau) your class name is Candidates and you have defined the constructor with Candidate without s, that is why it is being considered as a normal method, and thus should have a return type. You your rename your constructor as Candidates, or rename your class as Candidate which is better.



回答2:

Your Voter.setDetails function has no return type. If you don't want it to return specify the return type as void

public void setDetails(String name, int votNum, int precint)
{
    this.name = name;
    this.votNum = votNum;
    this.precint = precint;
}


回答3:

add a return type to all of your methods in your voter class.

Currently here in your code you have shown just one method showDetails() which has no return type. Certainly there will be other methods also for which you haven't declared a return type.



回答4:

invalid method declaration; return type required

Error message says it clearly; you need to give return type of each method. If there is no return type just give void.



回答5:

Method should have return type which says about the type of return value(if anything is returned from the method).

If nothing is returned, specify void.

This is exactly what is missing in your setDetails method.



标签: java oop