Results 1 to 4 of 4
  1. #1
    MemoNick is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Static Variables

    I'm working on an RPG, but I've hit a stumbling block - Static Variables. I searched in the Internet, but I'm not sure I understand completely.

    Let's say I have a Quest Class, and from the Main Class, I create two Quest Objects:

    Java Code:
    static Quest q, p;
    Now I have a Control class, and want to print out the quests' names:

    Java Code:
    public class Control {
        
        public void controlPrint() {
            System.out.println("1: " + Main.q.getQuestName());
            System.out.println("2: " + Main.p.getQuestName());
        }
        
    }
    If I don't make the Quest variables static, I can't access them. When I make them static, am I making the global? i.e. if I change their name from the Main, when I access them from the Control do I find their names changed?

    Thanks a lot in advance.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Static Variables

    Have you read the tutorial about variables explained here: Variables (The Java™ Tutorials > Learning the Java Language > Language Basics). And the other one on instance and class members here: http://docs.oracle.com/javase/tutori...classvars.html. If you haven't read it please take sometime to read it and ask for more if you still confuse about it.
    Last edited by wsaryada; 06-03-2012 at 04:13 PM.

  3. #3
    MemoNick is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: Static Variables

    Thanks for the links.

    this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.
    That cleared something, but I still don't understand everything.

    Let's say I have the Quest Class and I create an instance of it; quest q. What does that change if I make that static?
    Last edited by MemoNick; 06-03-2012 at 05:10 PM.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Static Variables

    Here is an snippet to test:

    Java Code:
    class Test {
        static Quest quest = new Quest();
     
        public static void main(String[] args) {
            Test t1 = new Test1();
            Test t2 = new Test2();
    
            if (t1.quest == t2.quest) {
                System.out.println("t1.quest is t2.quest);
            }
        }
    }
    From the example t1 and t2 is two different instance of Test. But as quest is declared static it become class variable, means it only exist as a single instance across all instance of the Test objects. So t1 and t2 will refer to the same quest.

    Note, the example above is not a proper way to access static variable though. To access an static / class member you should always use something like Test.quest. This make our program clearer that quest is a static member of Test class.
    Last edited by wsaryada; 06-04-2012 at 02:04 AM.

Similar Threads

  1. I can't able to see static variables:(
    By yuzi123 in forum New To Java
    Replies: 5
    Last Post: 11-24-2011, 06:36 AM
  2. Replies: 6
    Last Post: 02-15-2010, 04:31 AM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  4. Using Static Variables
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:08 PM
  5. Help with static variables
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 05:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •