stackoverflow error in java

前端 未结 6 1909
南方客
南方客 2020-12-20 19:44

I\'m a newbie in java. I\'m writing a class where the constructor must check the price parameter and ensure it is not a negative number. And if it is negative, it must set

相关标签:
6条回答
  • 2020-12-20 20:33

    Your getting infinite recursion, because your if condition checks your getprice() method, not your price variable.

    Many modern compilers will warn you when you have coded something that results in infinite recursion.

    I still sometimes come across this error too, especially with IDE's that have intellisense.

    Good luck learning Java! :)

    0 讨论(0)
  • 2020-12-20 20:35

    Your getprice() method calls itself instead of checking price. This is leading to an infinite recursion in this case.

    0 讨论(0)
  • 2020-12-20 20:35

    Ignacio has explained the cause, and the solution:

    Change the line

    if(getprice() < 0)
    

    to this:

    if(price < 0)
    
    0 讨论(0)
  • 2020-12-20 20:35

    Not a cure for the recursion problem, but you should also consider checking the price at construction time.
    Sometimes (most times?) it is better that your constructor fails with an Exception instead of allowing the construction of an inconsistent object. This way it is easier to localize such an Error.
    Example:

    public Book(String bookTitle, String bookAuthor, String bookCode, int bookPages, boolean paperback, double bookRetail)
    {
        if (bookRetail < 0.0)
            throw new IllegalArgumentException("negative bookRetail: " + bookRetail);
        ...
    }
    

    The risk is that your application can fail when in the production environment, which can be a mess. To avoid this, you can use an assert or, at least, give out or log the error and use some alternative. The assert check must be turned on for development and may be turned off at production. For Detail see Programming With Assertions

    public Book(String bookTitle, String bookAuthor, String bookCode, int bookPages, boolean paperback, double bookRetail)
    {
        assert bookRetail >= 0.0 : bookRetail;
        ...
    }
    

    or

    public Book(String bookTitle, String bookAuthor, String bookCode, int bookPages, boolean paperback, double bookRetail)
    {
        if (bookRetail >= 0.0) {
            price = bookRetail;
        } else {
            price = 0.0;
            // display or log the "illegal argument"
            Exception ex = new IllegalArgumentException("negative bookRetail: " + bookRetail);
            ex.printStackTrace();
        }
        ...
    }
    
    0 讨论(0)
  • 2020-12-20 20:39

    When you write a bean you generally want to check if the price being set is < 0, instead of making this calculation every time you try to get the variable.

    0 讨论(0)
  • 2020-12-20 20:50

    Your getprice should simply be written as :

    return price < 0 ? 0 : price;
    

    Btw, nice to see that a stackoverflow error is solved by stackoverflow.com

    0 讨论(0)
提交回复
热议问题