public class Solution {
public static void main(String[] args) {
//write your code here
Cat cat1 = new Cat();
Cat cat2 = new Cat();
System.out.println(cat1); // This gives output as the memory address of the object
}
public static class Cat {
System.out.println(cat1); // But when i commented the SOP statement in main method and try to print this one, on compilation it gives error <identifier expected> why?
}
}
Something interesting happened when trying to print Unable to understand
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
18 October 2020, 09:02
Outside methods in classes you can just declare fields or have a static block. And therefore the compiler expects an identifier so you can declare your field.
Btw. cat1 is unknown within the class so even if it would work (inside a static block) it would throw an exception.
The output you get is expected behaviour and later you'll learn more about it.
0
Dinesh
18 October 2020, 14:41
Thanks
0