0

Whenever I try to call a non static method, I always get the error "Non-static method cannot be referenced from a static context". Anyways this can be solved?

        
        int pages; 
        
        pages = content.length()/ 700; 
        
        if (content.length()%700!= 0) { 
            pages = pages+1;
        }
        return pages;
        
    } ```
```public double getPrice(int index){
        
        double price = 0;
        int pages = Book.getPages();
        
        if (index == 0){
            throw new IndexOutOfBoundsException();
            
        }
        else {
             price = (locationFactor * ()); 
             
        }
            
            
            
        
        return price;
    }```
lakes
  • 1
  • Which line in the code shown produces that error? Is it `Book.getPages()`? Where do you define the variable `Book`? If it's a class name and not a variable instance, that's the problem. Semantically, one would get the number of pages from a specific "book", not from the concept of a "book". – David May 05 '22 at 17:34
  • Does this answer your question: https://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods – Nick Bailey May 05 '22 at 17:35

0 Answers0