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

programming style

jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
Hi,

My query is related to code shown below. Here I have declared a few variables, like JFrame, JTextArea as instance variables where as a few, like JPanel and JButton, as local variables inside method go(). I could also have made all the variables as instance variables or all of these as local variables... It would work fine. Now my question is what is a better coding practice? How and where will this make a difference.

I am sorry if this question sounds very stupid, I am still new to programming.
Thank you all!



Still Learing..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
Generally, I would move all variables into a method that you don't need to use outside of that method. They just clutter up the namespace (using another field called "frame" anywhere else in the program might become confusing), and the objects they're pointing to might not become eligible for garbage collection.


Ping & DNS - updated with new look and Ping home screen widget
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
got yaa,
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: programming style
 
Similar Threads
Help with a simple gui
Doubt in GUI
JTextArea page 415 of Head First Java
From a OO Point of View in GUI design
change the texts to a different one in a JButton each time it is pressed.