What will be the output of the following code?

01: public class Foo {
02:     public static void main(String[] args) {
03:         class Horse {
04:             public String name;
05:             public Horse (String s) {
06:                 name  = s;
07:             }
08:         }
09:         Object obj = new Horse("Zippo");
10:         System.out.println(obj.name);
11:     }
12: }
Explanation
The code will fail to compile, because an object of type "Object" does not have a "name field". Casting the "obj" reference variable to "Horse" would fix the problem:

System.out.println(((Horse)obj).name);

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes