Results 1 to 2 of 2
Thread: Basics on collections
- 12-11-2011, 06:47 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 30
- Rep Power
- 0
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!
- 12-11-2011, 07:54 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: Basics on collections
A static member is only initialized once (when the class has just been loaded), so you can do this:
The LinkList class already implements the Collection interface because the List interface extends the Collection interface.Java Code:public class Cache { private static LinkedList<Member> mruList= new LinkedList<MemberL>(); ... }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Collections
By NatureFreshMilk in forum New To JavaReplies: 12Last Post: 05-23-2011, 08:12 AM -
Collections help.
By YoungJavaBoy in forum New To JavaReplies: 3Last Post: 03-14-2011, 11:14 PM -
Help collections
By nikosv in forum New To JavaReplies: 8Last Post: 12-13-2010, 05:29 PM -
Collections
By Cbani in forum New To JavaReplies: 3Last Post: 02-16-2010, 02:46 PM -
Collections Help
By Dr Gonzo in forum New To JavaReplies: 0Last Post: 12-07-2008, 09:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks