Why an object of a class is created inside a main method and it does not work if created inside a class without any method?