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 2 of 2

Thread: Must I add a Class Variable?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Must I add a Class Variable?

    There is this concept in c++, where i can store local variable by the use of static storage class. You see this is a powerful concept where if i want to iterate through elements of a class, i simply dont have to declare an instance variable as i do in java.
    I simply declare static const_iterator that will return the next element until i reach the
    end of the list.
    Why didnt java implement such a concept?


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Must I add a Class Variable?

    I'm not sure what you mean by 'iterate through elements of a class', but you can declare static member variables in Java that will be shared by all instances.

    So you can declare a static counter that is incremented in the constructor to track the number of instances, or a static list to which each new instance is added, so all instances are sequentially available, etc.

    You only have to read a Java primer to discover this, so I'm guessing you want to do something more - can you clarify your requirement?

Similar Threads

  1. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  2. Access and set variable in parent class through child
    By java_newbie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 19th, 2011, 11:44 PM
  3. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  4. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM