0

I have created a class, one instance variable is created inside main method and another is outside.

// calling a method from constructor
public class Raw4 {

    public static void main(String[] args) {
        Raw4 r = new Raw4();
    }

    Raw4 r1 = new Raw4();

}
deHaar
  • 17,687
  • 10
  • 38
  • 51
Anuj Patel
  • 39
  • 1
  • 5
  • This class is not complete if you are *calling a method from constructor* because there is no method in your code but the `main`. – deHaar Oct 23 '19 at 12:23
  • 3
    You create infinite r1, becuase each r1 will have an r1. Do you want it to be static? – matt Oct 23 '19 at 12:23

1 Answers1

2

When you create a new Raw4 object, all fields with initializers are initialized.

Your Raw4 class has a field named 'r1', of type Raw4, and it is initialized by creating... another Raw4 object.

So, the act of creating a Raw4 object involves creating a Raw4 object. That's a never-ending process.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72