Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor Question

 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone explain to me the meaning of the compile error as shown above? Thanks.
Clement
 
luco zhao
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Clement Ng:
Hi,

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

using
 
Eduard Manas
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response guys.
Clement
 
Eric Gonzalez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic