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: Help setting a private static class variable

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Help setting a private static class variable

    Hi there, I'm working on my assignment for uni and I'm having a few problems with one of the questions.
    So I'm trying to set the class variable currentLeader to an object of the LongJumper class, it should be null on first run.

       /*class variables */
     
       private static LongJumper currentLeader;

    then after the first jumper goes he should be set as the currentLeader, which I think I have under control but it's all coming unstuck when I try to use the private setter.

       /**
        * A private class setter method for the class variable currentLeader
        */
       private static void setCurrentLeader()
       {
         LongJumper.currentLeader = this;
       }

    My compiler has told me "non-static variable this cannot be referenced from a static context."

    I think know how to set it for an int, but setting it for an object has really thrown me

       private static void incremenInt()
       {
          class.int = class.int + 1;
       }

    I'd really appreciate if someone would be kind enough to offer me a few pointers on where I'm going wrong this, as I've pretty much run out of ideas and my course work doesn't seem to have included this type of scenario.

    Thanks

    edit. sorry if this is a little garbled I've been working on this for the past 6 hours


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help setting a private static class variable

    The keyword 'this' is used to refer to an instantiated object and cannot be used statically. To overcome this you can remove the static keyword from your function

       /**
        * A private class setter method for the class variable currentLeader
        */
       private void setCurrentLeader()
       {
         LongJumper.currentLeader = this;
       }

    You could also do this by creating a static setter with the object to set as the parameter
    Last edited by copeg; January 25th, 2010 at 08:06 PM.

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

    kyuss (January 26th, 2010)

  4. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help setting a private static class variable

    Thanks for replying copeg, unfortunately the question specifically asks for a class method to used for the setter.
    You could also do this by creating a static setter with the object to set as the parameter
    do you mean something along the lines of

    private static void setCurrentLeader(LongJumper aJumper)

    and if that's the case, how would I send a message from a LongJumper object with itself as the argument?

    eg
         if  (LongJumper.getCurrentLeader()== null)
         {
             setCurrentLeader(this.LongJumper);
         }

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Help setting a private static class variable

    Almost

    setCurrentLeader(this);

    // Json

  6. The Following User Says Thank You to Json For This Useful Post:

    kyuss (January 26th, 2010)

  7. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help setting a private static class variable

    thanks mate, I think the problem must be with my class setter method

       private static void setCurrentLeader(LongJumper aJumper)
       {
         LongJumper.currentLeader = aJumper;
       }

    My understanding is that when I call
    setCurrentLeader(this);
    the LongJumper referenced by "this" becomes "aJumper" which then becomes currentLeader. Unfortunately it's not working like that, as the currentLeader class variable stays null.

Similar Threads

  1. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 04:36 AM
  2. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 01:56 PM
  3. 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
  4. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM
  5. Problems in setting classpath
    By missyati in forum Java Theory & Questions
    Replies: 3
    Last Post: June 30th, 2009, 12:43 AM