Java error - "invalid method declaration; return type required"

java exception
java errors list
java error message
java error example
java error codes
java error types
java error class
syntax error in java

We are learning how to use multiple classes in Java now, and there is a project asking about creating a class Circle which will contain a radius and a diameter, then reference it from a main class to find the diameter. This code continues to receive an error (mentioned in the title)

public class Circle
{
    public CircleR(double r)
    {
        radius = r;
    }
    public diameter()
    {
        double d = radius * 2;
        return d;
    }
}

Thanks for any help, -AJ

Update 1: Okay, but I shouldn't have to declare the third line public CircleR(double r) as a double, right? In the book I am learning from, the example doesn't do that.

public class Circle 

    { 
        //This part is called the constructor and lets us specify the radius of a  
      //particular circle. 
      public Circle(double r) 
      { 
       radius = r; 
      } 

      //This is a method. It performs some action (in this case it calculates the 
        //area of the circle and returns it. 
        public double area( )  //area method 
      { 
          double a = Math.PI * radius * radius; 
       return a; 
    } 

    public double circumference( )  //circumference method 
    { 
      double c = 2 * Math.PI * radius; 
     return c; 
    } 

        public double radius;  //This is a State Variable…also called Instance 
         //Field and Data Member. It is available to code 
    // in ALL the methods in this class. 
     } 

As you can see, the code public Circle(double r).... how is that different from what I did in mine with public CircleR(double r)? For whatever reason, no error is given in the code from the book, however mine says there is an error there.

As you can see, the code public Circle(double r).... how is that different from what I did in mine with public CircleR(double r)? For whatever reason, no error is given in the code from the book, however mine says there is an error there.

When defining constructors of a class, they should have the same name as its class. Thus the following code

public class Circle
{ 
    //This part is called the constructor and lets us specify the radius of a  
    //particular circle. 
  public Circle(double r) 
  { 
   radius = r; 
  }
 ....
} 

is correct while your code

public class Circle
{
    private double radius;
    public CircleR(double r)
    {
        radius = r;
    }
    public diameter()
    {
       double d = radius * 2;
       return d;
    }
}

is wrong because your constructor has different name from its class. You could either follow the same code from the book and change your constructor from

public CircleR(double r) 

to

public Circle(double r)

or (if you really wanted to name your constructor as CircleR) rename your class to CircleR.

So your new class should be

public class CircleR
{
    private double radius;
    public CircleR(double r)
    {
        radius = r;
    }
    public double diameter()
    {
       double d = radius * 2;
       return d;
    }
}

I also added the return type double in your method as pointed out by Froyo and John B.

Refer to this article about constructors.

HTH.

Errors V/s Exceptions In Java, Error : An Error “indicates serious problems that a reasonable application should not try to catch.” Both Errors and Exceptions are the subclasses of java. lang. Throwable class. Errors are the conditions which cannot get recovered by any handling techniques. Install the latest Java version The continual redirection to java.com was a known issue affecting Java versions 7u25 and older. This issue was fixed with the 7u40 release of Java. To prevent the redirect to java.com, download the latest Java version from http://java.com.

Every method (other than a constructor) must have a return type.

public double diameter(){...

Types of Errors in Java with Examples, It is the JVM (Java Virtual Machine) which detects it while the program is running. To handle the error during the run time we can put our error code inside the try� java - Error: Selection does not contain a main type - Stack Overflow I am trying to run some java files in a new project. So I make the project, put the files in it and I try to run the main file so my game starts. I get an error that says selection does not conta

You forgot to declare double as a return type

public double diameter()
{
    double d = radius * 2;
    return d;
}

Class java.lang.Error, An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it. Internet Explorer (IE) has a new security feature, called ActiveX control blocking, to keep ActiveX controls, such as Java, up-to-date. This feature will prevent the app or web page from loading, if it finds that Java is out-of-date and will provide the option to update Java. Java (TM) was blocked because it is out of date

I had a similar issue when adding a class to the main method. Turns out it wasn't an issue, it was me not checking my spelling. So, as a noob, I learned that mis-spelling can and will mess things up. These posts helped me "see" my mistake and all is good now.

Java.lang.Errors, Errors provides for different errors thrown under java lang package. Error Summary. Sr.No. Error & Description. 1. AbstractMethodError. This is Thrown when an� Current Version Name Replaces Old Name Other Legacy Formats; Java 8 Java 8 Update x: JRE 8.0 JRE 1.8: 1.8.0_0x: Java 7 Java 7 Update x: JRE 7.0 JRE 1.7: 1.7.0_0x

Java Software Errors: How to Avoid 50 Code Issues in Java, Compiler error messages are created when the Java software code is run through the compiler. It is important to remember that a compiler may� Java try and catch The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block. The try and catch keywords come in pairs:

Error class in Java, You don't use Error in your code. An Error is a specific kind of Throwable , just as Exception is. Throwable is the base class that defines�

Java Exceptions (TryCatch), The technical term for this is: Java will throw an exception (throw an error). Java try and catch. The try statement allows you to define a block of code to be tested for�

Comments
  • Javac is usually quite helpful and the error messages it returns are quite clear. Next time you have one of these have a short break and the answer will come to you :)
  • CircleR is not a constructor for Circle. The names must match.
  • ok, but now it is highlighting the radius = r; portion and saying "cannot find symbol - variable class". and thanks for the article, i'm reading it now.
  • never mind, i realized that at the bottom, i need to say public double radius;. what is the reason for this, though? why do i state that at the bottom rather than right there inside of the public Circle(double r)?
  • Because... 'double r' is a parameter to the constructor. 'double radius;', outside of any method, is a field in the class. Parameters are discarded when the method returns. Fields exist as long as the class instance.
  • (P.S. 'private' would be more appropriate than 'public' for the 'double radius;' field.)
  • @Alex My bad, I should have noticed that your constructor's radius variable isn't defined anywhere. Anyways, I edited the code and added the private double radius field. Glad you liked the link I provided, here's another for defining methods in Java: tutorialspoint.com/java/java_methods.htm