aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion with constructor argument 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 "confusion with constructor argument" Watch "confusion with constructor argument" New topic
Author

confusion with constructor argument

Mamta Sharma
Greenhorn

Joined: Jun 03, 2008
Posts: 25
Hi all,
This question is from JavablackBelt-> Java SE Core - Intermed.

01: class Base {
02: private int value;
03:
04: Base (int v) {
05: value = v;
06: }
07:
08: public int getValue() {
09: return value;
10: }
11: }
12:
13: public class Derived extends Base {
14: int currentValue = 1;
15:
16: public Derived() {
17: super(currentValue);
18: currentValue++;
19: }
20: }
Options are:
A. getValue() returns 1 for all instances of Derived objects.
B. getValue() returns different values for all instances Derived objects.
C. This code fails to compile.
D. A runtime exception is raised when trying to create an instance of Derived object.

Correct answer is C -This code fails to compile.
Explanation : A constructor may only access final or static attributes, therefore super(currentValue) is incorrect.

Can anybody make it more simple for me,what i remember is we can call super with any variable its not necessary to be a static one.
Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
Hi Mamta,

If you try compiling this code, you get the following error:
"cannot reference currentValue before supertype constructor has been called super(currentValue);"
From this you understand that you cannot access instance variables of the subclass until the super class constructor runs.

In this case, currentValue is an instance member of Derived class and you tried to pass it to the super class constructor even before the object was created. That's why the code failed to compile.

The code would have worked had you changed the currentValue to be static. By making it static, it is no longer an instance variable, it is a class variable. As far as I know, only static variables can be passed as a part of a call to super().

Correct me if I'm wrong, but I don't think final variables can be passed to the super class constructor. Infact, when I compiled this code by making currentValue final, and I ended up with the same error as the one mentioned above.
Maybe the options were static and static final(without the comma), coz that works!

At the same time, I can pass a constant to super() and it would work fine.If you change the code to the following, it would work fine:




HTH,
Seema
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
The following would work, too:


SCJP 5 (98%) - SCBCD 5 (98%)
Mamta Sharma
Greenhorn

Joined: Jun 03, 2008
Posts: 25
Thanks both of you i have got my answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion with constructor argument