Results 1 to 8 of 8
  1. #1
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Instances Instantiated

    Suppose I have a class named Thread and an enum named Faction. The thread class has a constructor that takes in an enum array. I am wondering, how can I make a helper method that keeps track of the total number of Thread instances instantiated?

    Java Code:
    public enum Faction {
        A, BUNCH, OF, CONSTANTS, IS, IN, HERE
    }
    
    public class Thread { 
        public Thread(Faction[] enume) {
            ....
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Instances Instantiated

    You could do it with another class and and single instance of that class. Or you could think about what type of variable is independent of any particular instance of a class. Note that it is much easier to keep track of objects created than keeping track of objects that have been destroyed or garbage collected.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Instances Instantiated

    Quote Originally Posted by Robben View Post
    Suppose I have a class named Thread...
    Then my immediate advice would be to name it something else. You should always avoid a class name that already exists in the JDK, and never re-use a class name that's part of the java.lang package.

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

  4. #4
    Robben is offline Member
    Join Date
    Feb 2015
    Posts
    67
    Rep Power
    0

    Default Re: Instances Instantiated

    Quote Originally Posted by jim829 View Post
    You could do it with another class and and single instance of that class. Or you could think about what type of variable is independent of any particular instance of a class. Note that it is much easier to keep track of objects created than keeping track of objects that have been destroyed or garbage collected.

    Regards,
    Jim
    Can you elaborate please?

    Quote Originally Posted by DarrylBurke View Post
    Then my immediate advice would be to name it something else. You should always avoid a class name that already exists in the JDK, and never re-use a class name that's part of the java.lang package.

    db
    I used a random name, I had no idea thread was a keyword. But we can call it Spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Instances Instantiated

    Thread isn't a keyword. It's the name of a very well known class in the java.lang package.

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

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Instances Instantiated

    Quote Originally Posted by jim829 View Post
    You could do it with another class and and single instance of that class. Or you could think about what type of variable is independent of any particular instance of a class. Note that it is much easier to keep track of objects created than keeping track of objects that have been destroyed or garbage collected.
    Perhaps by applying the Closeable interface one might automate the latter using the try-with-resources feature of the Java language though.

    Much like how you would work with a JDBC connection pool since Java 7. But instead of maintaining a pool of objects, you maintain a reference count.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Instances Instantiated

    Of course it all depends what problem the OP is trying to solve by doing this.

    As I said in the other thread, I suspect there's a model/design flaw here somewhere.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Instances Instantiated

    That might be the case, but given how little the OP is sharing we can only assume he doesn't want assistance with that so we can at least do our best to help correctly develop what might ultimately be only a very educational mistake ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 7
    Last Post: 11-06-2013, 10:59 AM
  2. Using variables from a non-instantiated class
    By Skater901 in forum New To Java
    Replies: 4
    Last Post: 05-19-2012, 12:43 PM
  3. Class can't be instantiated.
    By 3maksim in forum Java Applets
    Replies: 12
    Last Post: 01-25-2012, 05:23 AM
  4. Circle is abstract; cannot be instantiated
    By hoangx87 in forum New To Java
    Replies: 20
    Last Post: 03-14-2011, 08:29 AM
  5. class is abstract; cannot be instantiated
    By Jamison5213 in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 09:16 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
  •