This tip is continuation of Static member classes and instance variables.
We will put together a code snippet to test it:
class Dolls { public static int dollCount = 25; protected static String doll = "doll"; private static int toyCount = 10; static String toy = "toy"; static void printTipResult () { System.out.println("access : true!"); } public static class Barbie{ void printVar () { System.out.println(doll + " : " + dollCount); System.out.println(toy + " : " + toyCount); printTipResult(); } } public static void main(String[] args) { Dolls.Barbie barbie = new Dolls.Barbie(); barbie.printVar(); }
}
What we have done above is defined 4 class variables with different access modifiers to test all scopes of static member variables. One static member method is also added to cover methods also.
When you run this code it won't generate compilation error or runtime error, the output will be:
doll : 25
toy : 10
access : true!
This proves today's Tip.

No comments:
Post a Comment