1
public class Bugs{
   private String bugType;
   private int legs;
   private int arms;
   private String nativeTo;

   public bug(String bt, int l, int a, String nt){
      bt=bugType;
      l=legs;
      a=arms;
      nt=nativeTo;
   }
}

Why do I keep getting a "invalid declaration method" here? It keeps saying a return method is needed.

4 Answers 4

2

The method that you want here is a special one called constructor. Constructors are used when you create new objects. In contrast to regular methods they don't have a declared return type.

You should change the line

public bug(String bt, int l, int a, String nt){

to

public Bugs(String bt, int l, int a, String nt){

it has to match the name of the class exactly.

You can read more about constructors here: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Sign up to request clarification or add additional context in comments.

Comments

2

Your constructor must be named the same as your class. Bugs, not bug. You are basically declaring a method without return type what is illegal.

Comments

1

Change public bug to public Bugs. This is because it is a constructor that must be the same as the class name.

Comments

1

change public bug to public Bugs if you want to make it a constructor. Otherwise if it is a method add a return statement before the last } of the method and also add a return type in the method.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.