• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Avoiding the "call to super must be first statement in constructor" message

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'd like to have something like that:



but I get the call to super must be first statement in constructor message...


Any way I can avoid that?

Thanks!
 
Floyd Montgomery
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, I can't modify the super class...
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you getting foo from? If it's an instance variable then the value will always be the default value.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't write super() twice, but you can get an if . . . then . . . else into a single statement, quite easily.
 
Floyd Montgomery
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:Where are you getting foo from? If it's an instance variable then the value will always be the default value.



I'm getting it from the session.
I should have written JSFUtil.getSession.getBean(...)
 
Floyd Montgomery
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can't write super() twice, but you can get an if . . . then . . . else into a single statement, quite easily.



Well yeah, I can use the ? and : symbols if I have two cases... but actually have four of them...
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's possible to chain multiple ? : together. But many people would consider that hard to read. You can also call a static method as part of the super constructor call:

Put whatever you want inside the method, as long as it doesn't access instance variables. That's why only static methods are allowed here; instance variables can't possibly have been meaningfully initialized at this point.
 
Floyd Montgomery
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Well, it's possible to chain multiple ? : together. But many people would consider that hard to read. You can also call a static method as part of the super constructor call:

Put whatever you want inside the method, as long as it doesn't access instance variables. That's why only static methods are allowed here; instance variables can't possibly have been meaningfully initialized at this point.




That works perfectly.
Thanks a bunch! : o)
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you create a factory method?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a side note, make sure methods called from constructors are private otherwise things can fail horribly if they get overridden.

Also, if it is a bean you should be able to call the no-arg super() then set the property in an if statement.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David O'Meara wrote:As a side note, make sure methods called from constructors are private otherwise things can fail horribly if they get overridden.


Even better, declare your class final.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But a final method has other effects. Easier to write safe code in the first place.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David O'Meara wrote:As a side note, make sure methods called from constructors are private otherwise things can fail horribly if they get overridden.


Private, static, or final, yes. Any of these will prevent overriding.

Stephan van Hulst wrote:Even better, declare your class final.


Unless of course you actually need it to be extensible. While extends may be often overused, it's still perfectly valid sometimes.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Floyd Montgomery wrote:Well yeah, I can use the ? and : symbols if I have two cases... but actually have four of them...

So why did you post an example with two cases? Please look at this FAQ.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic