Results 1 to 2 of 2
- 10-24-2012, 08:37 PM #1Member
- Join Date
- Oct 2010
- Rep Power
Is it bad programming practice to use Static Variables for accessing data?
Hello, I have recently written a program in Swing, however, I have yet to actually put any real data values in yet.
The plan was, I was going to make another Class filled with all my data values and just makes all the Values static to save me time.
I've always been told to avoid Static variables, but in terms of accessing data is it a bad idea? The rest of my swing code I have yet to use any Static variables and have relied on inheritance/Interfaces to access objects from other classes.
- 10-25-2012, 01:49 AM #2
Re: Is it bad programming practice to use Static Variables for accessing data?
Java is object oriented; static members should generally be limited to constants and utility methods. Also generally, data comprises state, and should not be static.
Inheritance should not be misused to gain access to members of another class. The key to a properly designed inheritance hierarchy is the "is-a" Using the Swing hierarchy as an example, a JLabel is-a JComponent with added functionality; if your subclass does not fulfill the is-a relationship of its superclass, that's misuse of inheritance. Pass references around instead and call methods of the other class to access its state.
If I haven't expressed that clearly enough (I'm not a programmer and have absolutely no qualifications in CS), ask and another more qualified member will surely step in to fill the gaps.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- By littlebirdpoo in forum New To JavaReplies: 1Last Post: 02-18-2012, 03:58 PM
- By Anerxomounos in forum New To JavaReplies: 3Last Post: 05-27-2011, 07:47 PM
- By ribbs2521 in forum New To JavaReplies: 4Last Post: 10-22-2009, 05:45 PM
- By porchrat in forum New To JavaReplies: 9Last Post: 04-08-2009, 12:54 PM
- By ravian in forum New To JavaReplies: 7Last Post: 03-01-2009, 11:09 PM