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

Thread: How do I set a static variable??

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How do I set a static variable??

    Hi all.

    I'm working on a problem for a school project.

    Bascially I have to set a static variable within a class. I have declared the variable as follows:-
    private static Horse theLeadingHorse;
    I now have to get and set this static variable by using two class methods. The setter has to be declared as private and the getter as public.

    how do I do this??? I have been racking my brains for hours now but no luck!! it's really starting to do my head in.

    Basically the getter class method has to return the variable and the setter class method has to set this horse as the leader (ie. the winner).

    Anyone any ideas or suggestions?????

    Any help would be greatly appreciated.

    thanks in advance

    John
    Last edited by helloworld922; January 20th, 2010 at 01:12 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do I set a static variable??

    implement the setter method non-statically. This way it will set the variable based on the instance information with each appropriate horse object. Then, implement the getter method statically so you can access it without having to go through any horse objects, but rather through the main class itself. To access static methods/fields: it's just like accessing instanced variables/methods except you use the class name.

    public class Horse
    {
         // static initialization
         private static String winner = "none";
         public static String getWinner()
         {
              return winner;
         }
    }
    public class Main
    {
         public static void main(String[] args)
         {
              // obviously, it will say "none" right now, but you can add this somewhere latter in you app and if winner has changed, it will reflect that at call time
              Horse.getWinner();
         }
    }

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do I set a static variable??

    Hi,

    Thanks for the suggestion but basically in my project I have to do it a certain way.

    basically the static variable has to be one of the Horse classes. In other words the variable will hold a class.

    Then the getter method will return which class is stored in the variable theLeadingHorse.

    (It's hard to explain)

    The setter method bascially sets the variable (theLeadingHorse) to a class.

    I tried doing it as follows:-
    public static Horse getTheLeadingHorse()
    	{
    	    return Horse.theLeadingHorse
     
    	}
     
    private void setCurrentLeader()
    	{
                                     // no idea how to set theCurrentLeader to hold the name of the leadingclass!!!
                          }
    Am i doing this totally wrong???
    Last edited by helloworld922; January 20th, 2010 at 01:42 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do I set a static variable??

    Hmm... If I understand you correctly, theLeadingHorse should hold a Horse object, not a Horse class (kind of a big difference). In this case, you can do what it is I said but with Horse objects rather than String objects. I'm guessing setCurrentLeader is called from the horse object that's currently the leader:

    private void setCurrentLeader()
    {
         Horse.theLeadingHorse = this;
    }

    Also, fyi in your code above you're missing a semi-colon in the getTheLeadingHorse() method.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    wingchunjohn (January 22nd, 2010)

  6. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do I set a static variable??

    Thankyou so much!!

    This is exactly what I was looking for. All the books etc I have don't show this! I've been having trouble with it for a while. thanks!

Similar Threads

  1. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 01:56 PM
  2. static final object
    By kalees in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 12:29 PM
  3. quite small (make the changelog non-static, i.e. load its content from a file)
    By Abdallah in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 30th, 2009, 09:00 PM
  4. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 11:10 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM