File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Global Components/Good Programming Practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Global Components/Good Programming Practice" Watch "Global Components/Good Programming Practice" New topic

Global Components/Good Programming Practice

Gulshan Singh
Ranch Hand

Joined: Apr 22, 2009
Posts: 41
So I have a program with a lot of JTextFields and JLabels, and they need to be accessed by all of the functions in a class. I've also learned that its bad programming practice to have a lot of global variables/components. So what's worse programming practice, making about 20 components global, or having your entire program in one long function? Or is there a way to avoid these problems?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

if they need to be accessed by all the methods of the class, then you should make them member variables of the class:

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Global Components/Good Programming Practice
It's not a secret anymore!