Results 1 to 2 of 2
  1. #1
    blug is offline Member
    Join Date
    Oct 2010
    Rep Power

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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

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

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Good Practice Programming Websites?
    By littlebirdpoo in forum New To Java
    Replies: 1
    Last Post: 02-18-2012, 02:58 PM
  2. Object data overlapping.- Need help with static variables.
    By Anerxomounos in forum New To Java
    Replies: 3
    Last Post: 05-27-2011, 07:47 PM
  3. Replies: 4
    Last Post: 10-22-2009, 05:45 PM
  4. [SOLVED] Is this good programming practice?
    By porchrat in forum New To Java
    Replies: 9
    Last Post: 04-08-2009, 12:54 PM
  5. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10:09 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