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

Constructor Question

Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,

Can someone explain to me the meaning of the compile error as shown above? Thanks.
Clement


best regards,<br />Steven<br />SCJP, SCEA
luco zhao
Ranch Hand

Joined: Apr 23, 2002
Posts: 50
Originally posted by Clement Ng:
Hi,

Can someone explain to me the meaning of the compile error as shown above? Thanks.
Clement

using


Great thanks,<br />Luco Zhao
Eduard Manas
Ranch Hand

Joined: May 12, 2002
Posts: 69
Originally posted by Clement Ng:
Hi,

Can someone explain to me the meaning of the compile error as shown above? Thanks.
Clement


Clement
The reason why your code will not compile is because objects need to be initialised from the top of the class hierarchy down.
In order to ensure this, Java requires that the first statement of a constructor is always either a call to super() or this().
In your example, the variable "a" is used before any call to super(). This means that the superclass has not been initialised yet, and consequently the variable "a" have not yet been initialised.
If you make the "a" variable static this will make a valid code. However, you will be changing the "meaning" of that variable, as it will now be a class variable rather than an instance variable.
Hope this helps
Eduard
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Thanks for the response guys.
Clement
Eric Gonzalez
Greenhorn

Joined: May 01, 2002
Posts: 10
I dont understand.
you talk about the superclass when class cn7 doesn't extends any one.
Are you refering to the implicit inheritence from Object?
Please me tell me if I'm right:
a) this( argument list) call a same class constructor -used to avoid to rewrite code-(not necessary it have to involve inheritence
b) super(arguments list) call the baseclass constructor to initialize the hidden baseclass object passed secretly to the subclass
Eduard Manas
Ranch Hand

Joined: May 12, 2002
Posts: 69
Originally posted by Eric Gonzalez:
I dont understand.
you talk about the superclass when class cn7 doesn't extends any one.
Are you refering to the implicit inheritence from Object?
Please me tell me if I'm right:
a) this( argument list) call a same class constructor -used to avoid to rewrite code-(not necessary it have to involve inheritence
b) super(arguments list) call the baseclass constructor to initialize the hidden baseclass object passed secretly to the subclass

Eric,
As you guessed the superclass of cn7 is Object. Remember that all classes that do not explicitly extend any other class will extend from Object.
Regarding to your queries in this and super, "this (args)" is used to call constructors of the same class. Note that this is the only way to call another constructor ("cn7(a)" would be incorrect).
The call super(args) is used to call a constructor of the superclass. As you said, this will initialise it.
Just another note, when the java compiler detects that the class does not explicitly extends another class, it will automatically add an "extends Object".
Hope this helps
Eduard
 
wood burning stoves
 
subject: Constructor Question