• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding super() and this()  RSS feed

 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm preparing for the OCA 7 exam. I learnt from the OCA 7 book by K&B that you are only allowed to access static variables and methods as a part of the call to super() or this(). I think I would find it easier to understand if they had given a complete example but they just gave (super(Animal.NAME); // OK because NAME is static.) but I am not finding the complete code in the book. So if anyone could give a complete example I would be really grateful. Also another query..Is this actually used in real-world programs?
 
Rehan Zahoor
Ranch Hand
Posts: 85
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is a simple example of use of static variable in constructor of class Animal. Note that no argument constructor calls the other constructor using static variable.
This technique is used in java classes. There can be many occasions when use of static variable in the super() or this() is the most appropriate choice.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rehan Zahoor wrote: . . . There can be many occasions when use of static variable in the super() or this() is the most appropriate choice.
Really? Please explain why that is an appropriate use of a static field.

I think OP has misunderstood the passage in the book. Please tell us exactly what the book says about static fields being only permitted in this(...) and super(...).
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no passage. It written as just one sentence or point and it goes like this:
Only static variables and methods can be accessed as part of the call to super() or this().(Example:super(Animal.NAME) is OK, because NAME is declared as a static variable)
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That may be as opposed to accessing instance variables and methods as part of a call to super() or this().
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:There is no passage. It written as just one sentence or point and it goes like this:
Only static variables and methods can be accessed as part of the call to super() or this().(Example:super(Animal.NAME) is OK, because NAME is declared as a static variable)


Because the rules of Java state that you can't access a Child's attributes before the supertype's constructor has been called (they don't exist before then).
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yeah, quite frankly, I never understood the restriction -- as it can be easily circumvented via a getter method. Obviously, the instance variables exists at the call, and they have default values.

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Yeah, quite frankly, I never understood the restriction -- as it can be easily circumvented via a getter method. Obviously, the instance variables exists at the call, and they have default values.

Henry


Not in the super() or this() call, eg super(getId()). That won't compile.

Yes, if you have an abstract method which the supertype calls in its constructor. But then that's considered a no no anyway.

Frankly I'm not concerned as passing a subtype's attributes into a supertype strikes me as just plain wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!