Originally posted by Lovleen Gupta: ...My understanding says that since y has a default access, it will be accessible to class Minor and hence we can use it...
This has nothing to do with access controls. It has to do with trying to use the variable before it's initialized.
In main, you are calling the constructor for Minor(). The first line within this constructor is a call to super(int x). The int being passed is y. That's the problem: There is no "y" at this point. The reason is that y is an instance variable in Uber, and until Uber's constructor finishes, there is no Uber instance. Or as the compiler puts it: "cannot reference y before supertype constructor has been called."
On the other hand, if you use the commented-out line and make y static (a class variable), then this will work. The reason is that the Uber class is initialized (making its static members available) when it's loaded, which happens before the constructor call.
To see the order in which these things happen, add some println statements with some static and non-static initializer blocks...
PS: You might find Code Tags and indentation helpful too.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
posted 11 years ago
Thanks a lot Marc. It was ver y helpful.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database