Results 1 to 4 of 4
- 10-04-2011, 03:01 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 34
- Rep Power
- 0
Abstract Class for one static variable?
Hi,
i have an abstract class with 3 classes that extend that class. I now need a static variable that 2 of these classes need to share. Currently i do that by having another abstract class that extends the first abstract class and from that the other two classes extend. There is only this 1 variable in the in between abstract class. But im asking myself if this is the "correct" way to do (Making a class for only 1 variable)?
- 10-04-2011, 03:05 AM #2
Senior Member
- Join Date
- Jul 2010
- Posts
- 127
- Rep Power
- 0
Re: Abstract Class for one static variable?
You could just create a new class that extends nothing (except Object of course), and have it have your static variables there. If these classes are ran across multiple threads, make sure to declare them volatile
-
Re: Abstract Class for one static variable?
This smells of a global variable. Why are you doing this? Why not pass information through method or constructor parameters? Tell us the logical problem are you are trying to solve, not how you plan to code it.
- 10-04-2011, 03:32 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 34
- Rep Power
- 0
Re: Abstract Class for one static variable?
The 3 classes are for grabbing (stock)-information from the internet periodically - 2 of them use the same source. Because of not hammering the source(-server) with requests i have a delay for when the request starts. So the first instance starts it request with a 0 delay the next with 1 sec, the next after 2 secs, and so on. Cause i have 3 classes with 2 sources i need 2 different delay's (one for each source). So i made a static delay variable as stated in the first post that is increased each time a new instance is made. Passing it as a constructor parameter would be a pain cause i would have to keep track of the number of made instances of the 2 classes that share the same delay variable (from the same source) and the number of instances of the 3rd (different source). I would need 2 delay variables (one for each source) of which i give one as a constructor parameter depending on the instance i make. And for each source i could potentially add in the future i would need another delay variable i keep track of.
Similar Threads
-
non-static variable grade cannot be referenced from a static context
By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 10:59 AM -
accessing a one class's non static variable from another class
By aruna1 in forum New To JavaReplies: 6Last Post: 03-31-2009, 05:27 AM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 11:51 AM -
Abstract Class with Static Methods
By bugger in forum New To JavaReplies: 7Last Post: 09-05-2008, 01:20 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 10:25 PM
Bookmarks