Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Counter

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Counter

    Hello everyone,
    I have this project for homework where I need to create one super class(Solids) and three subclasses (Cylinder, Cone and Sphere). The exercise says "... there will be a counter for each type of solid, so that we know how many have been created (of each type)...". My question is, is there a way that I can create the counter ONCE and then use it for all the subclasses? If so, how do I do that? (NOTE: I am familiar with the homework policy and I am not asking for the code, just a hint or something).
    Thank you


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Counter

    In each subclass declare a static variable counter.
    Increment it in the constructor itself
    for e.g
    class Cylinder extends Solids{
    static int counter1;
    Cylinder(){
    counter1=counter1+1;
    }

  3. The Following User Says Thank You to hurrah For This Useful Post:

    mssim (November 7th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Counter

    Static, yes. Thanks

Similar Threads

  1. Counter troubles
    By GinoGore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 09:28 AM
  2. Help With Counter
    By Catgroove in forum Java Theory & Questions
    Replies: 7
    Last Post: February 10th, 2011, 08:50 AM
  3. Dice Counter
    By Xxl0n3w01fxX in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 26th, 2011, 11:49 AM
  4. Odd Even Zero Counter?
    By velop in forum Java Theory & Questions
    Replies: 1
    Last Post: February 19th, 2010, 02:13 PM
  5. String counter
    By chkang0130 in forum Algorithms & Recursion
    Replies: 9
    Last Post: December 8th, 2009, 11:56 AM