Results 1 to 7 of 7
- 12-01-2010, 03:03 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
Static methods or individual classes?
My swing application file has quite a few static methods which I plan to remove by creating a separate class for each static method in the same folder and then use the methods of these classes in my application file. Does it matter which approach I take? If yes, which approach is better - using a lot of static methods in my app OR writing a class for each static method?
- 12-01-2010, 03:11 PM #2
Why would you write a class for each static method? What do these methods do?
It matters which approach you take. But only to you. We can't really tell you what approach is best, especially without seeing any code (no, that doesn't mean paste all your code here). Try an approach. Does it work?
- 12-01-2010, 03:23 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
The app structure looks something like this:
public class MyApp
{
static JMenuBar createMenuBar()
{
// create menu bar
}
static JToolBar createToolBar()
{
// create toolbar
}
static JLabel createStatusBar()
{
// create status bar
}
ActionListener ac = new ActionListener()
{
// handling swing events...
};
JFrame myFrame = new JFrame();
myFrame.setVisible(true);
...
...
}
- 12-01-2010, 03:28 PM #4
I guess the question is, why are these methods static in the first place?
Dollar bet: You did that to resolve "non-static XYZ referenced from static context" exceptions, didn't you? DIDN'T YOU. :p
- 12-01-2010, 03:33 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
er... yes. Is that an incorrect methodology?
- 12-01-2010, 03:43 PM #6
Yes and no. It's common for a "newbie" (or even a mid-level or slightly experienced) programmer to use the "quick fix" for that kind of Exception without actually understanding what the problem is.
I am betting you actually have something like this:
When really you should have something more like:Java Code:public class MyApp { static JMenuBar createMenuBar(){ // create menu bar } static JToolBar createToolBar(){ // create toolbar } static JLabel createStatusBar(){ // create status bar } public static void main(String [] args){ JFrame frame = new JFrame(); //pseudocode, but you get the point frame.add(MyApp.createMenuBar()); frame.add(MyApp.createToolBar()); frame.add(MyApp.createStatusBar()); } }
That's just guessing at what you're doing, but I'm hoping it demonstrates the point.Java Code:public class MyApp { public JMenuBar createMenuBar(){ // create menu bar } public JToolBar createToolBar(){ // create toolbar } public JLabel createStatusBar(){ // create status bar } public static void main(String [] args){ JFrame frame = new JFrame(); MyApp appInstance = new MyApp(); //pseudocode, but you get the point frame.add(appInstance.createMenuBar()); frame.add(appInstance.createToolBar()); frame.add(appInstance.createStatusBar()); } }Last edited by KevinWorkman; 12-01-2010 at 03:53 PM.
- 12-01-2010, 03:52 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Call Static Methods Outside Classes
By Insomniac Riot in forum New To JavaReplies: 4Last Post: 05-11-2010, 10:03 PM -
static vs. non-static nested classes
By rinke in forum Advanced JavaReplies: 8Last Post: 06-30-2009, 07:15 AM -
Recursion with static and non static methods
By sh4dyPT in forum New To JavaReplies: 14Last Post: 03-27-2009, 06:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks