Hello ~ ----------------------------------------------------------------------------Code: package test1; public class Test1{ static int x=42; }
package test2; public class Test2 extends test1.Test1{ public static void main(String[] args){ System.out.println("X=n"+x); } } Why it compilation fails? Thanks
Originally posted by Hu Peng: Hello ~ ----------------------------------------------------------------------------Code: package test1; public class Test1{ static int x=42; }
package test2; public class Test2 extends test1.Test1{ public static void main(String[] args){ System.out.println("X=n"+x); } } Why it compilation fails? Thanks
x has got default access which means that its invisible outside its own package..But if you change access modifier to protected,it will work[It gets access through inheritence]..
Just a gentle note... it would help in the future if you also posted the exact compilation error message. for something as small as this, it might not matter much, as it's fairly easy to see. But the bigger your code gets, the more important it becomes.
those error messages often give a pretty big clue as to what the problem is, so it's a good idea to start learning what they mean.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards