Why am I having Errors on the Method?

I don't understand why I'm getting these errors in the setInitialQuantity method declaration, does anyone mind taking a look?

public class Invoice
{
    private int quantity;
    private String partNum;
    private String partDesc;
    private double unitPrice;


    //constructor 
    public Invoice ( int initialQuantity, String partNumber, String partDescript, double unitPrice)
    {
        public void setInitialQuantity ( int initialQuantity )
        {
            quantity = initialQuantity;
        }   
    }//end Invoice constructor
}

Errors:

javac Invoice.java
Invoice.java:16: illegal start of expression
    public void setInitialQuantity ( int initialQuantity )
    ^
Invoice.java:16: illegal start of expression
    public void setInitialQuantity ( int initialQuantity )
           ^
Invoice.java:16: ';' expected
    public void setInitialQuantity ( int initialQuantity )
                                  ^
Invoice.java:16: ';' expected
    public void setInitialQuantity ( int initialQuantity )
                                                        ^
4 errors

Answers


You can't have a method inside another (or a Concstructor) in Java, move it outside the constructor but inside the class.

public class Invoice
{
    private int quantity;
    private String partNum;
    private String partDesc;
    private double unitPrice;


    //constructor 
    public Invoice ( int initialQuantity, String partNumber, String partDescript, double unitPrice)
    {
    }//end Invoice constructor

public void setInitialQuantity ( int initialQuantity )
    {
            quantity = initialQuantity;
    }   

}

You can't define method inside another method.

//constructor     
public Invoice(int initialQuantity,String partNumber,String partDescript,double unitPrice)
{
   this.quantity=initialQuantity;
   this.partNum=partNumber;
   this.partDesc=partDescript;
   this.unitPrice=unitPrice;
}   
public void setInitialQuantity(int initialQuantity)
{
    quantity = initialQuantity;
} 

Need Your Help

Is there any way to get rid of these spring/aspectj warnings when building the project?

spring build warnings aop aspectj

I've frequently put up with this for a long time, but I'm a little worried that it's slowing my build process down now. There are a good few seconds that are taken up as Spring/AspectJ reports these