jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question of the day Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question of the day" Watch "Question of the day" New topic
Author

Question of the day

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Why doesn't this compile? What change can you make to instanceVariable to get it to compile?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
The SuperClass constructor invocation statement, super, is processed before the new instance of Test class has been created. For that reason, the arguments to the superclass constructor invocation statement can not legally refer to members that have not been created and initialized. These include:
- Instance variables declared in this class or any superclass.
- Instance methods declared in this class or any superclass.
- The keyword this.

Legal references include:
- Static variables declared in the subclass class or any superclass.
- Static methods declared in the subclass or any superclass.
So, all we have to do is make instanceVariable static.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Calvin Y
Greenhorn

Joined: Apr 21, 2003
Posts: 3
I don't think so.
You just need to add a super(int i) before other constructors of subclass Test.
These codes failed to compile because it lacks the default constructor of super class,which prevent the object of superclass to get a new instance.
Plz correct me if I made a wrong reply


I will do the best with what God gave me
suresh guru
Ranch Hand

Joined: Sep 07, 2002
Posts: 38
I think this code dosnt compile because the superclass is unable to refer the instance Variable.
all we have to do is that we can make the variable static
pinky yadav
Ranch Hand

Joined: Jun 17, 2002
Posts: 44
hi,
As most of you suggested we cannot access the instance variable of a sub class in the constructor of superclass because till the constructor of the super class returns the instance variables are not executed.

JLS 8.8.5 says
Finally, if the constructor invocation statement is a superclass constructor invocation and the constructor invocation statement completes normally, then all instance variable initializers of C and all instance initializers of C are executed.

Pinky
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Thomas

oo........


Francis Siu
SCJP, MCDBA
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
I'm not sure of the answer, probably has something to do with the var not being static, but I don't think it has anything to do with the missing constructor in the last post because I beleive the default constructor is always implied even if not written.
Please correct me if I'm wrong
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Damien
humm.....
In this case
If you declear a constructor in that class,the compiler would not give a
free gift (default one)
to you in the same class,so you need to write an another one in that class.
Anyway,Thomas may be worry about something which lead him to neglect writting the constructor.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I don't need a default constructor in the SuperClass because the constructor is always getting invoked by the constructors of its child class. In fact, adding a default consructor to the SuperClass does not get rid of the error. You can't have a super(1) and a this(1) in the same constructor so that doesn't help either. Changing Test() to run super() doesn't help because the Test(int i) is also getting an error.
As some wrote, the underlying error is that you can not use an instance variable in an explicit constructor invocation using this() or super().
The rule is that the arguments in an explicit constructor invocation (this(myVar) or super(myVar)) are executed in a static context. If myVar does not exist in a static context then you can't use myVar as an argument. What would exist in a static context? Either myVar is static or myVar is a method variable.


or

[ April 22, 2003: Message edited by: Thomas Paul ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question of the day
 
Similar Threads
Subclass Inheritence for protected variable
Static and Non-static question
constructor
doubt in constructor
over ridden method and super constructor