• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Regarding super() and this()

 
Ranch Hand
Posts: 89
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 85
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80954
522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Sheriff
Posts: 17735
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic