File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Structural question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Structural question" Watch "Structural question" New topic
Author

Structural question

Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
When building applications with swing, is it considered bad to make the components class variables? It's difficult to access them otherwise without making it very difficult to add more components.
I'd like to make my JTextAreas private class variables, but is that "bad" coding?
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
I have used such type of code..with private JTextFields and JLabels etc.
I dont see any reason why it could be bad code..
:roll:


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

It's perfectly fine to ise member variables to hold references to components; in fact, there isn't realy any alternative. But you said class variables, or "static" variables in Java. This is not especially good style; it leads to code like this:



This is bad because it's procedural, because it makes the code hard to reuse, because it leads to very long, hard to modify methods, etc.

There's an easy alternative, and it looks like this:



[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Structural question
 
Similar Threads
Question about writing an app using OOP
Java's Inner Classes - Does those Help or is better Avoid those?
Static and Global variables
Problem with static variables on a specific server
Is initializing mem vars proper