• Post Reply Bookmark Topic Watch Topic
  • New Topic

change default behaviour in constructor  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

i have a class that looks like this:



and now i extend this class. its constructor gets a new parameter which one i want to use in the initMethod, like this:

you see, i override the "initMethod" to init something with the help of the new parameter "myNewParameter" but this will go wrong since its not initialized yet. problem is, i cant initialize the new parameter before i call the super-constructor. this is really annoying. is there a workaroung to this? and no, i dont want to call initMethod by hand from the outside, this is what constructors are for.

regards, garz
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Why do you want a workaround? You are doing something hazardous, calling a non-private method from a constructor. To avoid such problems, any methods called from a constructor should be marked private or final.
 
garz faust
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are doing something hazardous, calling a non-private method from a constructor.

why is this hazardous?

i want to explain my problem again with words only:
the class i'm using has to be initialized. when i extend the class, the intializing process stays the same but there are some more things to be initialized with the help of new parameters. since i'm forced to call a super constructor and i'm forced to call it in the first place i'm not able to extend my init-process with the help of new parameters respectively if i would declare "initMethod" private i couldnt extend the init-process at all because of no visibility in the subclass. so i'm forced to call the old init-process in the first place and make new stuff afterwards. but in my case, this isnt possible, because the init-process is always the same and extensions have to be done in certain places in that init-process.

i could make the "initMethod" private and let it call protected mehtods which i override in subclasses to alter init-behavior. its like this anyway but i wanted to keep the example simple. or is this hazardous too?
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
garz faust wrote:why is this hazardous?


You already posted an example of why it's hazardous. When it happened to me, the phrase that came to mind wasn't "hazardous", it was "damn nuisance", but in any case you've seen the problem. Java doesn't let you control the order of initialization, and sometimes the official order of initialization is inconvenient.

Eventually what I had to do was stop doing initialization in the constructor. And in fact I gave up on letting those objects be constructed by strangers completely. Instead I provided a factory method which constructed an object and then called its init() method (in which all of the actual initialization took place) and then returned that object to the caller.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Paul C has answered much better than I would have. Thank you.
 
garz faust
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you both for your answers, very much appreciated.

Eventually what I had to do was stop doing initialization in the constructor.

yes you are right, thats the only solution i can imagine too but i hoped that there was some java trick-things that i could do. i dont like this solution. i think java should do better.

why do i have to call the super constructor at the first position by force? what is the sense behind this? in python i can call the superconstructor anywhere i want to or even leave it out. so whats the sense behind this?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!