Your main method creates an instance of Cloning ( Cloning c=new Cloning(); ), which causes the instance variable c be initialized ( Cloning c=new Cloning(); ), which creates another instance of Cloning and so on ...
You have an infinite chain of constructor calls, which leads to a StackOverflowError .
In the above code, I have a simple class and an instance of the class
You do not have an instance of the class. You have an instance level instance. If you want an instance of the class, change
Cloning c=new Cloning();
to
static Cloning c=new Cloning();
source share