Results 1 to 4 of 4
  1. #1
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default 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)?

  2. #2
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default 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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  4. #4
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default 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

  1. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  2. Replies: 6
    Last Post: 03-31-2009, 04:27 AM
  3. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  4. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 12:20 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •