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

How LOLbad is this stylistically?

 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is based on one of the examples in Head First Java... it plays some notes using MIDI, and each time a MIDI controller event is fired, it draws a colored rectangle on screen.

Instead of using their rather klugey method of drawing shapes, I created an inner class RectShape so that I could associate some random dimensions with a color, and also conveniently store them in an ArrayList, so that I can redraw the whole lot when the panel needs repainting.

The important bits are the bottom two sections of code.

2 questions about my RectShape class:

1) Is it really bad just to declare its fields and values like this, without using a constructor method?

2) Is it OK to use the fields directly in my JPanel's paintComponent(...) method, since it's an inner class? Or should I still use getters / setters (seems a bit long-winded).

To me it seems more elegant to make the concisest code possible, but you guys might disagree...

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For #2, that's really fine as it stands, there's nothing wrong with that. It's pretty much expected that inner classes will directly exchange data with their parents, in one direction or another.

For #1, that's a little harder. Normally, there's no reason not to initialize data members directly if possible, but here, those expressions are awfully long, and makes me a little uncomfortable seeing that big solid block of code at class scope. If it were me, I might define some methods to make those expressions shorter -- for example, a method random(int) that returned a random number less than the given value.

Further, it strikes me as somewhat inconvenient that there's no way to make a "plain" or "default" RectShape, but only to make one containing random values. It would be nice to be able to make one with standard values for testing, for example. This would suggest having multiple constructors: one that makes a random RectShape, and one that initializes to a given list of values.
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the comments. I changed the RectShape class as you suggested:


Netbeans is alerting me that I have an "overridable method call in constructor" for the random() method... not sure if I should be worried about that but it seems to run OK...
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just put a "final" in front of the int random(..) and that gets rid of the warnings...
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The warning is telling you that if you were to define a subclass of RectShape, and it were to override random() in such a way that it used the chld class's member variables, then that override could be called at a point before those member variables were initialized -- which is potentially bad. You can make the warning go away by making this impossible; make RectShape final, make random() final, or make random() private. Any of these would prevent random() from being overridden.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luigi Plinge wrote:I just put a "final" in front of the int random(..) and that gets rid of the warnings...



Looks like you figured out the solution while I was explaining it!
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google is my friend
 
Marshal
Posts: 79979
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote: . . . that override could be called . . . which is potentially bad. . . . make random() final, or make random() private. . . .

Somebody else had a similar problem, which you can read about here.
 
This tiny ad is wafer thin:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic