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

Thread: putting a non static variable in a static variable

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default putting a non static variable in a static variable

    I am trying to move the content of xp to skillXp, and I am getting this error "Cannot make a static reference to the non-static field xp"

    How can I put the contents of non static var xp, into static var skillXp?

    Any help or advice would be great

    Here's what I have
     
    private final double xp[] = new double[SKILL_NUMBER];
    public static double skillXp[] = new double[SKILL_NUMBER];
     
    	public static void setNewXP (int skillid) {
     
     
    		skillXp[skillid] = xp[skillid];
     
     
    	}


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: putting a non static variable in a static variable

    Why do you want to do this? This seems a strange way to organize your code.

    Note that in the static world xp has no meaning as it simply doesn't exist. It requires an instance of the class to exist. So to fully answer your question you need to explain a lot more about what is going on and what you're trying to do.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: putting a non static variable in a static variable

    I'd like to be able to access xp in another class

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: putting a non static variable in a static variable

    Quote Originally Posted by beennn View Post
    I'd like to be able to access xp in another class
    That's not how you do this, by using statics, and in fact you should strive to avoid use of statics as much as possible except in certain situations where it is useful (and this is not one).

    To have another class be able to interact with this field, that other class needs an instance of an object of this class, and it can then get the information by calling a method on the instance, or it can alter the object's behavior by calling a method. Again static is most definitely not the way to go.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: putting a non static variable in a static variable

    I attempted to make an instance of the Skill class which contains the variables, however I was getting "The constructor Skill() is undefined"

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: putting a non static variable in a static variable

    Quote Originally Posted by beennn View Post
    I attempted to make an instance of the Skill class which contains the variables, however I was getting "The constructor Skill() is undefined"
    Then you'll need to look at the Skill class code and only use constructors that are available for use.

Similar Threads

  1. Replies: 4
    Last Post: November 15th, 2012, 12:09 AM
  2. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  3. non-static variable
    By frozen java in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 15th, 2011, 06:18 PM
  4. non-static variable, addActionListener()
    By bobbyrne01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 10:58 AM
  5. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 04:36 AM