hi
please see the below code
class staticdemo
{
String name="shenba";
}
class statictest
{
static staticdemo obj; public static void main(String arg[])
{
System.out.println(obj.name);
}
}
in the statictest class i create reference obj of type staticdemo.but the obj is not referring any object. so during when i try to access the name variable using obj it throws null pointer exception.
but in the case of enum the same thing is not giving any issues.
enum animal
{
dog("woof"), cat("meow");
String sound;
animal(String s)
{
sound=s;
}
public String getSound()
{
return sound;
}
};
class enumdemo1
{
static animal a; public static void main(String arg[])
{
System.out.println(a.dog.sound);
System.out.println(a.dog.getSound());
System.out.println(a.cat.sound);
System.out.println(a.cat.getSound());
}
}
static animal a; In the above code value for a is not assigned.ie some thing like animal.dog is not assigned.but the code a.dog.sound is not giveing any exception during run time.
enum also a special type of class only right? then why different behaviour in enum?