• 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

Dealing with uninitialized final fields

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 202 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, there is a code sample that uses final fields in a constructor:

Since when can you pass parameters into a constructor without declaring them as instance variables first?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have to set an instance field with the parameters in a constructor.  They just go away, but it this case, they are used to set volume.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Peterson wrote:Since when can you pass parameters into a constructor without declaring them as instance variables first?



Let's just clarify what you're talking about there. The constructor has three declared parameters, named "length", "width", and "height". And values are passed into that constructor by some other code which isn't shown in your example. That might look like this:



This passes the values 20, 11, and 9 into the constructor, and none of those values are declared as instance variables.

But clearly that isn't what you meant. My best guess is that you mean that the declared parameters of the constructor are supposed to match instance variables of the class. Like this:



It looks like the first constructors you saw were written like this, to assign values to instance variables of the class. This is certainly a common use of constructors. However it also looks like you've decided that there is a rule that all constructors must look like that. But there's no such rule, as you can see from the example.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried the code to see whether it compiles?

Remember that this is a cert exam preparation book; cert exam questions often incorporate features likely to confuse the unwary. It wouldn't have confused you had the code said this:-I have introduced some new problems. Not only would that documentation comment make the question too easy, but also it exposes private implementation details, and describes a weakness of your design in such a manner as to make that weakness awkward to correct I shall let you make a suggestion as to how you would improve the documentation comment so the weakness is easier to correct.

The strange parameter problem is no significant difference from this sort of method in my Kettle class:-There is no degrees field, but the degrees parameter is used to alter the value of the temperature field without heating the water hotter than boiling. In the case of MouseHouse, the field is final, so it must not be assigned in a method.

Adding discussion to our exams forum.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Peterson wrote:On page 202 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, there is a code sample that uses final fields in a constructor:
Since when can you pass parameters into a constructor without declaring them as instance variables first?



Since always. Modern compilers will generally whine if you never actually use parameters, whether directly or indirectly, but never in any programming language that I've ever heard of were parameters required to be assigned to fields in any context.

Note that the difference between "const" (like in C/C++) and "final" is that a const must be assigned at the point of definition, whereas a final is only required to be be assigned before its first use. Or in the case of a constructor setting a final field, the end of the constructor, whichever comes first.

 
Happily living in the valley of the dried frogs with a few tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic