Here I declare an instance of an animal class in the same class. B c Considered an error:
struct demo{ int anyvar; struct demo anyvar1; };
since it should be an infinite ad loop.
Then, why is this code allowed in Java?
class Animal{ Animal object1 = new Animal(); public static void main(String[] args) { Animal obj = new Animal(); obj.dostuff(); } public void dostuff() { System.out.println("Compiles"); object1.dostuff(); } public void keepdoingstuff() { System.out.println("Doing Stuff..."); object1.keepdoingstuff(); } }
source share