Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Iterative method returning an object

Out of curiosity I came to write the following method:

public Object getObject() {
    return this.getObject();        
}

Why would Java allow me to write this? This method would never return any Object and would result in a StackOverflow Error.

like image 806
Luke Taylor Avatar asked Feb 20 '23 05:02

Luke Taylor


1 Answers

It shouldn't stop you from doing it - this is just bottomless recursion, but it could've been a regular one. The compiler doesn't need to go and understand to code to guess if it's properly defined recursion or not.

like image 164
Bozho Avatar answered Mar 03 '23 20:03

Bozho