This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Access Modifiers For The GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Reply Bookmark "Access Modifiers For The GUI" Watch "Access Modifiers For The GUI" New topic
Author

Access Modifiers For The GUI

Simon Quirke
Greenhorn

Joined: Jul 28, 2004
Posts: 3
Hello Again JavaRanch,

In my Bodgitt and Scarper GUI I create the main JFrame in the Constructor which in turn calls a number of methods to create and populate various panels. Some of the method signatures are given below.



Most of the methods depend on instance variables such as:



My question is what access modifiers are the most appropriate for a Swing GUI? Should I specify everything as protected so my classes could be extended at a later date or is default more appropriate.

Thoughts and comments appreciated

Simon
Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
IMHO, this sort of translates to the question: What is the likelihood that my class will be reused?

If your class is reused you might *still* want controlled access to your instance variables by supplying getXXX() methods like getMenuBar, getMainTable, getNameField, etc...

Particularly, look at Swing as is... if I recall correctly, Swing doesn't allow access to its instance variables either.

But truth be told, for this project I'd just leave them private and not worry about inheritence so much. Unless you're building something like a specialized JTextField, in which case, yeah, g'head and publish accessor methods.


SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
Simon,
IMO, one should not assume that his classes will be extended unless it were designed and documented for inheretance. If you intentionally designed your classes and document it for inheretance, then it should be public or protected. However, if you didn't designed it for inheretence, then your classes should be private of default modifier.
Inheretance can produce bugs if it is used inproperly. I always favore Composition and Forwarding over Inheretance.


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
What's forwarding?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Modifiers For The GUI
 
Similar Threads
JMenu help
GUI and events
How do Singleton class? Is it even possible?
passing member field value between classes
public vs. private modifier