Results 1 to 2 of 2
  1. #1
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Basics on collections

    Hi guys,

    I'm trying to think of the most intelligent way to do the following: I want to write a class that contains a collection Cache of objects of type Member. The Member class is nested within the Cache class. Different classes should be able to call the Cache, but I want to maintain a single static set of the elements going into the Cache. A few questions arise in this case:

    1. How do I declare the set to be used within the Cache? (Right now, when declaring global field for my class, I have
    private static LinkedList<CacheMember> mru_list = null; as a global field for the Cache class, but does this mean that every time the Cache class is called, my static list is getting reset to null? How can I avoid this? (I tried simply writing
    private static LinkedList<CacheMember> mru_list; at the declaration of my global field, but the problem is that I heard it was bad practice to leave a field uninitialized, so I wanted to avoid doing this...))

    2. Is the Cache class begging to implement the Collection interface? (This isnít a school project, Iím simply trying to become a better programmer, so Iíd like to follow good logical form.)

    Any feedback would be greatly appreciated!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,351
    Blog Entries
    7
    Rep Power
    20

    Default Re: Basics on collections

    A static member is only initialized once (when the class has just been loaded), so you can do this:

    Java Code:
    public class Cache {
       private static LinkedList<Member> mruList= new LinkedList<MemberL>();
       ...
    }
    The LinkList class already implements the Collection interface because the List interface extends the Collection interface.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Collections
    By NatureFreshMilk in forum New To Java
    Replies: 12
    Last Post: 05-23-2011, 08:12 AM
  2. Collections help.
    By YoungJavaBoy in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 11:14 PM
  3. Help collections
    By nikosv in forum New To Java
    Replies: 8
    Last Post: 12-13-2010, 05:29 PM
  4. Collections
    By Cbani in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 02:46 PM
  5. Collections Help
    By Dr Gonzo in forum New To Java
    Replies: 0
    Last Post: 12-07-2008, 09:15 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
  •