File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes public vs. private modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "public vs. private modifier" Watch "public vs. private modifier" New topic
Author

public vs. private modifier

Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Hi,

in my MVC-view class I have a reference to the main window. In my main window the gui is created with all its components, like textfields etc.
In my view class I have defined the getter/setter methods to access the content of the components in the textfields in main window. (Small example, see below).
Is it ok to set the modifier of my gui components in the main window to public? Or do I also have to define getter/setter methods there?
The reason I want to avoid all the setter and getter methods is, that my MainWindow class is already quite big, about 500 lines.



SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2359
    
    7

Hi Bob, when you mention main window you mean the JTable screen right? Or are you referring to user inputs to set up where the database file and such?

If it's the JTable window, my main window class is quite small. I have JMenuBar, JTable, button JPanel and status bar. Each of these Swing components are in their own class. Delegation to those classes.

And best practice and encapsulation/OO principle sake, if the variable is "instance variable" then it should be private, never public. If different packages protected. It's like using least privilege security but with Java it's more like least access.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Hi K. Tsang,

thanks for answering.

K. Tsang wrote:Hi Bob, when you mention main window you mean the JTable screen right? Or are you referring to user inputs to set up where the database file and such?

Right. I talk about the main window of the application, which includes of the JTable, the text fields, the buttons, the menu etc.
My createAndShowGUI method is about 150 lines big. I have heard that 150 line is the limit. My only other methods set the preferred column width in the table and update the selected room/row also in the table. Beside that I have some private classes.

K. Tsang wrote:
If it's the JTable window, my main window class is quite small. I have JMenuBar, JTable, button JPanel and status bar. Each of these Swing components are in their own class. Delegation to those classes.

Mmmh. Do I understand you correct, that you have an "own made" class for each of the swing components like e.g. buttons, that creates the component and has some getter methods and if needed some other methods? And in the mainWindow class you only use these delegate methods.
I sound like a repeater to you? LOL
So do you create the actual layout of the gui in your panel class and delegates it to the mainWindow? And you pass the needed components as parameter to the panel constructor?
Sounds interesting.

K. Tsang wrote:
And best practice and encapsulation/OO principle sake, if the variable is "instance variable" then it should be private, never public. If different packages protected. It's like using least privilege security but with Java it's more like least access.

Yeah, that's true. I changed the public modifier to private and includes some getter methods (I don't need setters).

Thanks and Cheers
Bob
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

Hi Bob,

I have heard that 150 line is the limit.

Who told you that? My main window class is exactly 489 lines (comments included), 215 lines were needed to create my simple GUI.

The private data members of my main window are the swing-components you see in my main window (the JTable, search-button, book-button,...), so I can easily access these components from within other methods (in this class of course). I didn't declare any getters or setters to retrieve or modify the content of these components. And in my constructor I create the main window, but the sub-panels I need in my main window (like the search-panel) are created in seperate (private) methods, so the code stays clear, understandable and is easy to maintain (because of the code seperation: if you have to extend the search-panel for example, you just have to look into the appropriate private method)

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Hi Roel,

Roel De Nijs wrote:
I have heard that 150 line is the limit.

Who told you that? My main window class is exactly 489 lines (comments included), 215 lines were needed to create my simple GUI.

I got that from checkStyle. Of course I meant the method size (to avoid confusion). But you can configure that, so I guess the limit is up to you. I just wasn't sure.

Roel De Nijs wrote:
The private data members of my main window are the swing-components you see in my main window (the JTable, search-button, book-button,...), so I can easily access these components from within other methods (in this class of course). I didn't declare any getters or setters to retrieve or modify the content of these components. And in my constructor I create the main window, but the sub-panels I need in my main window (like the search-panel) are created in seperate (private) methods, so the code stays clear, understandable and is easy to maintain (because of the code seperation: if you have to extend the search-panel for example, you just have to look into the appropriate private method)

Nice solution with the different methods for the sub-panels. Maybe I will steal this idea.

Thanks and cheers.
Bob
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2359
    
    7

My main GUI class is exactly 160 lines long with comments. I have a method for displaying the JTable and another for displaying other components.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5201
    
  12

K. Tsang wrote:My main GUI class is exactly 160 lines long with comments.

But you use a lot of helper classes, so that's a bit of cheating

Kind regards,
Roel
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Roel De Nijs wrote:
K. Tsang wrote:My main GUI class is exactly 160 lines long with comments.

But you use a lot of helper classes, so that's a bit of cheating
l

K. Tsang won the line number contest

Thanks guys for your priceless help. Got some ideas from you.

cheers
Bob
Dennis Leong
Greenhorn

Joined: Jul 08, 2009
Posts: 9
hiya all,
getting off-topic, but how many lines do you think the MainWindow should be before you may lose marks?
reason i ask is mines about 1000 lines (including comments), mainly due to the fact my JTable is feature rich with a CellRenderer, custom traversal policy, key/mouse listeners, and all sorts of bells and whistles. I would have much preferred to have subclassed JTable, but the consensus on here was that's not a good idea as it goes against the instructions. Yet having a large View class is not as maintainable. i mentioned all this in choices.txt.
cheers,


cheers,
D
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2359
    
    7

Hello Dennis,

Honestly think about this way. From an OO perspective, which is better - more modularized classes or single big class? Also consider maintainability and ease of understanding (eg for junior programmers). Cohesion vs coupling concept.

In your case, are you using inner classes for you JTable cell renderers and listeners etc? As you can see from the above posts, my main window is pretty small and used helper classes. I too had 2 JTable cell renderers for date and cost columns, each has its own class.

However, as long as you have an explanation why you did such a thing then you should be fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public vs. private modifier