What exactly does “static” mean when declaring “global” variables in Java?












7















I've been running into this problem many times and I never bothered to learn why its happening and learn what "static" actually means. I just applied the change that Eclipse suggested and moved on.



public class Member {

// Global Variables
int iNumVertices;
int iNumEdges;

public static void main(String args) {

// do stuff

iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

// do more stuff

} // main end
}


So eclipse tells me to do static int iNumVertices; and I'm not sure why. So what exactly is "static", how is it used, what is the purpose of using "static", and why is it giving me this problem?










share|improve this question




















  • 4





    Those aren't global variables. They're not even public. Even if they were public they wouldn't be global variables. Even if they were public and static they wouldn't be global variables. Java doesn't have a concept of global. Everything is declared either locally or in a class or interface.

    – Mark Peters
    Aug 5 '10 at 15:36













  • @Mark... I'm sorry? I wasn't aware of these facts. Thanks for the clarification. So how would you categorize them?

    – Hristo
    Aug 5 '10 at 15:43













  • @Hristo: I think "member variables" is the correct term.

    – torak
    Aug 5 '10 at 15:53











  • It is probably a good idea to deal with C/C++ as a separate question.

    – doron
    Aug 5 '10 at 16:02











  • @deus-ex... thanks. I'll re-post as a separate question.

    – Hristo
    Aug 5 '10 at 16:03
















7















I've been running into this problem many times and I never bothered to learn why its happening and learn what "static" actually means. I just applied the change that Eclipse suggested and moved on.



public class Member {

// Global Variables
int iNumVertices;
int iNumEdges;

public static void main(String args) {

// do stuff

iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

// do more stuff

} // main end
}


So eclipse tells me to do static int iNumVertices; and I'm not sure why. So what exactly is "static", how is it used, what is the purpose of using "static", and why is it giving me this problem?










share|improve this question




















  • 4





    Those aren't global variables. They're not even public. Even if they were public they wouldn't be global variables. Even if they were public and static they wouldn't be global variables. Java doesn't have a concept of global. Everything is declared either locally or in a class or interface.

    – Mark Peters
    Aug 5 '10 at 15:36













  • @Mark... I'm sorry? I wasn't aware of these facts. Thanks for the clarification. So how would you categorize them?

    – Hristo
    Aug 5 '10 at 15:43













  • @Hristo: I think "member variables" is the correct term.

    – torak
    Aug 5 '10 at 15:53











  • It is probably a good idea to deal with C/C++ as a separate question.

    – doron
    Aug 5 '10 at 16:02











  • @deus-ex... thanks. I'll re-post as a separate question.

    – Hristo
    Aug 5 '10 at 16:03














7












7








7








I've been running into this problem many times and I never bothered to learn why its happening and learn what "static" actually means. I just applied the change that Eclipse suggested and moved on.



public class Member {

// Global Variables
int iNumVertices;
int iNumEdges;

public static void main(String args) {

// do stuff

iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

// do more stuff

} // main end
}


So eclipse tells me to do static int iNumVertices; and I'm not sure why. So what exactly is "static", how is it used, what is the purpose of using "static", and why is it giving me this problem?










share|improve this question
















I've been running into this problem many times and I never bothered to learn why its happening and learn what "static" actually means. I just applied the change that Eclipse suggested and moved on.



public class Member {

// Global Variables
int iNumVertices;
int iNumEdges;

public static void main(String args) {

// do stuff

iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

// do more stuff

} // main end
}


So eclipse tells me to do static int iNumVertices; and I'm not sure why. So what exactly is "static", how is it used, what is the purpose of using "static", and why is it giving me this problem?







java class static instance-variables static-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 14 '13 at 8:45







Hristo

















asked Aug 5 '10 at 14:35









HristoHristo

24.1k54146213




24.1k54146213








  • 4





    Those aren't global variables. They're not even public. Even if they were public they wouldn't be global variables. Even if they were public and static they wouldn't be global variables. Java doesn't have a concept of global. Everything is declared either locally or in a class or interface.

    – Mark Peters
    Aug 5 '10 at 15:36













  • @Mark... I'm sorry? I wasn't aware of these facts. Thanks for the clarification. So how would you categorize them?

    – Hristo
    Aug 5 '10 at 15:43













  • @Hristo: I think "member variables" is the correct term.

    – torak
    Aug 5 '10 at 15:53











  • It is probably a good idea to deal with C/C++ as a separate question.

    – doron
    Aug 5 '10 at 16:02











  • @deus-ex... thanks. I'll re-post as a separate question.

    – Hristo
    Aug 5 '10 at 16:03














  • 4





    Those aren't global variables. They're not even public. Even if they were public they wouldn't be global variables. Even if they were public and static they wouldn't be global variables. Java doesn't have a concept of global. Everything is declared either locally or in a class or interface.

    – Mark Peters
    Aug 5 '10 at 15:36













  • @Mark... I'm sorry? I wasn't aware of these facts. Thanks for the clarification. So how would you categorize them?

    – Hristo
    Aug 5 '10 at 15:43













  • @Hristo: I think "member variables" is the correct term.

    – torak
    Aug 5 '10 at 15:53











  • It is probably a good idea to deal with C/C++ as a separate question.

    – doron
    Aug 5 '10 at 16:02











  • @deus-ex... thanks. I'll re-post as a separate question.

    – Hristo
    Aug 5 '10 at 16:03








4




4





Those aren't global variables. They're not even public. Even if they were public they wouldn't be global variables. Even if they were public and static they wouldn't be global variables. Java doesn't have a concept of global. Everything is declared either locally or in a class or interface.

– Mark Peters
Aug 5 '10 at 15:36







Those aren't global variables. They're not even public. Even if they were public they wouldn't be global variables. Even if they were public and static they wouldn't be global variables. Java doesn't have a concept of global. Everything is declared either locally or in a class or interface.

– Mark Peters
Aug 5 '10 at 15:36















@Mark... I'm sorry? I wasn't aware of these facts. Thanks for the clarification. So how would you categorize them?

– Hristo
Aug 5 '10 at 15:43







@Mark... I'm sorry? I wasn't aware of these facts. Thanks for the clarification. So how would you categorize them?

– Hristo
Aug 5 '10 at 15:43















@Hristo: I think "member variables" is the correct term.

– torak
Aug 5 '10 at 15:53





@Hristo: I think "member variables" is the correct term.

– torak
Aug 5 '10 at 15:53













It is probably a good idea to deal with C/C++ as a separate question.

– doron
Aug 5 '10 at 16:02





It is probably a good idea to deal with C/C++ as a separate question.

– doron
Aug 5 '10 at 16:02













@deus-ex... thanks. I'll re-post as a separate question.

– Hristo
Aug 5 '10 at 16:03





@deus-ex... thanks. I'll re-post as a separate question.

– Hristo
Aug 5 '10 at 16:03












8 Answers
8






active

oldest

votes


















9














Here's your example:



public class Member {

// Global Variables
int iNumVertices;
int iNumEdges;

public static void main(String args) {

// do stuff

iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

}
}


The method main is a static method associated with the class. It is not associated with an instance of Member, so it cannot access variables that are associated with an instance of Member. The solution to this is not to make those fields static. Instead, you need to create an instance of Member using the new keyword.



Here's a modified version:



public class Member {
// Fields
private int iNumVertices;
private int iNumEdges;

public Member(){
// init the class
}

public static void main(String args) {
Member member = new Member();
member.iNumVertices = 0;
// do more stuff
}
}


Finding yourself creating global statics is an indication to you that you should think carefully about how you're designing something. It's not always wrong, but it should tell you to think about what you're doing.






share|improve this answer
























  • ohhh... I see. That makes a lot of sense. Why did you decide to make the 2 global variables private? Is it because they belong to the Member class and should only be accessed by Member objects?

    – Hristo
    Aug 5 '10 at 14:46






  • 3





    It is a best practice to make everything private that does not need to be accessed from outside, even in a small test project with only one class.

    – f1sh
    Aug 5 '10 at 14:48






  • 2





    @Hristo It's my belief that in general, every instance variable (also called fields, properties, and some other names) should be private. You want to expose as little of the internal workings of your class as possible. This helps you make your class more predictable because you won't have someone else modify the state of your class. 6 months down the road when you're fixing a bug in your code, you won't have to worry about 5 other files changing your variables if you keep them private.

    – Jonathon Faust
    Aug 5 '10 at 14:51






  • 1





    .. Thanks for response. That makes sense and it seems like its good practice to keep them private. Luckily, this isn't a long term project... its a short solution to a puzzle I'm working on, so it won't be going on a huge scale. But I'll keep this in mind for future projects :)

    – Hristo
    Aug 5 '10 at 14:57



















4














static variables are those that are shared across all objects of a class. Here in your example for every object of Member you create , you will get objects that have it's own iNumVertices values. When you use static with a variable, there is only one variable shared across every object of Member. static methods work the same way - they are shared across all objects.



Since static variables/methods are common to all objects, one need not make an object of the class to access these variables/methods.



Non-static variables like iNumVertices belong to an object of a class. They cannot be accessed without creating an object. So when you access a non-static variable from a static context (here main method), then java wouldn't know which object's iNumVertices you are trying to accesss. Hence the error.



Either make iNumVertices static, or refer to it by creating an object of Member



Member m = new Member();
m.iNumVertices = 0;





share|improve this answer
























  • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

    – Hristo
    Aug 5 '10 at 14:55











  • Exactly! That is exact purpose of member variables. You must not, however, confuse it with scope of a variable - public, private etc.

    – Ara
    Aug 5 '10 at 15:10













  • wait... I thought scope referred to the time range in which a variable lives. Are you saying that public, private, etc... also define the scope?

    – Hristo
    Aug 5 '10 at 15:22






  • 2





    public, private deals with inheritance and where all a variable/method is visible. static deals exclusively with sharing of variables within objects of a class. Scope is word used in both cases, but in completely different meanings.

    – Ara
    Aug 5 '10 at 15:31











  • interesting... I haven't encountered this before. I have always dealt with scope referring to the lifetime of a variable/object/etc... Thanks for your explanation.

    – Hristo
    Aug 5 '10 at 15:35



















4














learn what "static" actually means


What static actually means that Class variable will be same for all instance of that particular class, however if you want to avoid using static variables(which is a good idea, since static variables are being kept in memory) you can pass variable value trough constructor thereby avoiding usage of static modifier, and achieve the same effect(that is if you pass the same value upon class instantiation).



Here is code example :



public class Car{

private int speed;

public Car(int speed){
this.speed = speed;
}

}


So you can do this when creating new instance :



Car car = new Car(100);


and every time you create Car instance it will have speed 100, thus avoiding static declaration private static int speed = 100;






share|improve this answer


























  • ahh that's interesting... slightly different solution from the other answers. mind posting some code to demonstrate?

    – Hristo
    Aug 5 '10 at 15:29











  • @Hristo I added some code

    – Gandalf StormCrow
    Aug 5 '10 at 15:36











  • Thanks for the code.

    – Hristo
    Aug 5 '10 at 15:58






  • 1





    @Hristo if you want your example to work, you need to create function which sets the value of iNumVertices to 0(so called setter function). Then inside main you should instantiate your class Member member = new Member();, then call setter function to change the value of your class field, member.setINumVertices(0);

    – Gandalf StormCrow
    Aug 5 '10 at 16:06











  • .. ahh right. That makes total sense. Thanks!

    – Hristo
    Aug 5 '10 at 16:13



















2














Static variables are class variables. There will be a single copy of that variable avaiable to all instances of the class and they will share that variable. Static members can also be used without referencing a specific instance of the class.



More here:



http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html






share|improve this answer































    1














    Static variables do not need to have the class be instantiated in order to be accessed, so if you are trying to access a non-static variable form a static context you are risking trying to access something that has not been initialized/instantiated.






    share|improve this answer































      1














      Static methods can access only static variables. There are two kinds of variables in class. one is static variables(also class variables) and other is instance variable. Only one copy of Static variable exists in memory but instance variables will be instantiated for each object. So for static variables all objects access the same variable and any change made by one object will be reflected to other objects.
      The question here is why is that methods have to be static to access static variables. When you make a method static, you can access the method without instantiating objects of that class. So if this method is able to access instance variables then for which object's variables should it make change to? The other way is possible ie non static methods can access static variables.






      share|improve this answer
























      • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

        – Hristo
        Aug 5 '10 at 14:58






      • 1





        @Hristo Correct. The term "instance" refers to a unique, individual realization of the class. The class describes how each instance should be structured. Statics sidestep that and aren't really part of the individual instances. They can't be unique to an instance -- there's only one copy of a static, not one associated with each instance.

        – Jonathon Faust
        Aug 5 '10 at 15:05











      • Thanks for the explanation. That makes sense.

        – Hristo
        Aug 5 '10 at 15:21



















      0














      Every class variable (a variable declared within the class body and outside the method bodies) used in a static method needs to be declared static too.



      Static class variables and methods can be accessed outside that class without the need for an instance of that class.






      share|improve this answer































        0














        static variables are common to all instances of a Class.



        Note: As said earlier these are class variables i.e. shared by all instances.



        These can also be called as class level variables.
        Generally you define Constants(You will also need final keyword for defining constants) and Global variables as static.



        For more information refer:
        http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html



        http://www.roseindia.net/java/beginners/staticvariable.shtml






        share|improve this answer

























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3415781%2fwhat-exactly-does-static-mean-when-declaring-global-variables-in-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          8 Answers
          8






          active

          oldest

          votes








          8 Answers
          8






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          9














          Here's your example:



          public class Member {

          // Global Variables
          int iNumVertices;
          int iNumEdges;

          public static void main(String args) {

          // do stuff

          iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

          }
          }


          The method main is a static method associated with the class. It is not associated with an instance of Member, so it cannot access variables that are associated with an instance of Member. The solution to this is not to make those fields static. Instead, you need to create an instance of Member using the new keyword.



          Here's a modified version:



          public class Member {
          // Fields
          private int iNumVertices;
          private int iNumEdges;

          public Member(){
          // init the class
          }

          public static void main(String args) {
          Member member = new Member();
          member.iNumVertices = 0;
          // do more stuff
          }
          }


          Finding yourself creating global statics is an indication to you that you should think carefully about how you're designing something. It's not always wrong, but it should tell you to think about what you're doing.






          share|improve this answer
























          • ohhh... I see. That makes a lot of sense. Why did you decide to make the 2 global variables private? Is it because they belong to the Member class and should only be accessed by Member objects?

            – Hristo
            Aug 5 '10 at 14:46






          • 3





            It is a best practice to make everything private that does not need to be accessed from outside, even in a small test project with only one class.

            – f1sh
            Aug 5 '10 at 14:48






          • 2





            @Hristo It's my belief that in general, every instance variable (also called fields, properties, and some other names) should be private. You want to expose as little of the internal workings of your class as possible. This helps you make your class more predictable because you won't have someone else modify the state of your class. 6 months down the road when you're fixing a bug in your code, you won't have to worry about 5 other files changing your variables if you keep them private.

            – Jonathon Faust
            Aug 5 '10 at 14:51






          • 1





            .. Thanks for response. That makes sense and it seems like its good practice to keep them private. Luckily, this isn't a long term project... its a short solution to a puzzle I'm working on, so it won't be going on a huge scale. But I'll keep this in mind for future projects :)

            – Hristo
            Aug 5 '10 at 14:57
















          9














          Here's your example:



          public class Member {

          // Global Variables
          int iNumVertices;
          int iNumEdges;

          public static void main(String args) {

          // do stuff

          iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

          }
          }


          The method main is a static method associated with the class. It is not associated with an instance of Member, so it cannot access variables that are associated with an instance of Member. The solution to this is not to make those fields static. Instead, you need to create an instance of Member using the new keyword.



          Here's a modified version:



          public class Member {
          // Fields
          private int iNumVertices;
          private int iNumEdges;

          public Member(){
          // init the class
          }

          public static void main(String args) {
          Member member = new Member();
          member.iNumVertices = 0;
          // do more stuff
          }
          }


          Finding yourself creating global statics is an indication to you that you should think carefully about how you're designing something. It's not always wrong, but it should tell you to think about what you're doing.






          share|improve this answer
























          • ohhh... I see. That makes a lot of sense. Why did you decide to make the 2 global variables private? Is it because they belong to the Member class and should only be accessed by Member objects?

            – Hristo
            Aug 5 '10 at 14:46






          • 3





            It is a best practice to make everything private that does not need to be accessed from outside, even in a small test project with only one class.

            – f1sh
            Aug 5 '10 at 14:48






          • 2





            @Hristo It's my belief that in general, every instance variable (also called fields, properties, and some other names) should be private. You want to expose as little of the internal workings of your class as possible. This helps you make your class more predictable because you won't have someone else modify the state of your class. 6 months down the road when you're fixing a bug in your code, you won't have to worry about 5 other files changing your variables if you keep them private.

            – Jonathon Faust
            Aug 5 '10 at 14:51






          • 1





            .. Thanks for response. That makes sense and it seems like its good practice to keep them private. Luckily, this isn't a long term project... its a short solution to a puzzle I'm working on, so it won't be going on a huge scale. But I'll keep this in mind for future projects :)

            – Hristo
            Aug 5 '10 at 14:57














          9












          9








          9







          Here's your example:



          public class Member {

          // Global Variables
          int iNumVertices;
          int iNumEdges;

          public static void main(String args) {

          // do stuff

          iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

          }
          }


          The method main is a static method associated with the class. It is not associated with an instance of Member, so it cannot access variables that are associated with an instance of Member. The solution to this is not to make those fields static. Instead, you need to create an instance of Member using the new keyword.



          Here's a modified version:



          public class Member {
          // Fields
          private int iNumVertices;
          private int iNumEdges;

          public Member(){
          // init the class
          }

          public static void main(String args) {
          Member member = new Member();
          member.iNumVertices = 0;
          // do more stuff
          }
          }


          Finding yourself creating global statics is an indication to you that you should think carefully about how you're designing something. It's not always wrong, but it should tell you to think about what you're doing.






          share|improve this answer













          Here's your example:



          public class Member {

          // Global Variables
          int iNumVertices;
          int iNumEdges;

          public static void main(String args) {

          // do stuff

          iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices

          }
          }


          The method main is a static method associated with the class. It is not associated with an instance of Member, so it cannot access variables that are associated with an instance of Member. The solution to this is not to make those fields static. Instead, you need to create an instance of Member using the new keyword.



          Here's a modified version:



          public class Member {
          // Fields
          private int iNumVertices;
          private int iNumEdges;

          public Member(){
          // init the class
          }

          public static void main(String args) {
          Member member = new Member();
          member.iNumVertices = 0;
          // do more stuff
          }
          }


          Finding yourself creating global statics is an indication to you that you should think carefully about how you're designing something. It's not always wrong, but it should tell you to think about what you're doing.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 5 '10 at 14:42









          Jonathon FaustJonathon Faust

          10.6k24258




          10.6k24258













          • ohhh... I see. That makes a lot of sense. Why did you decide to make the 2 global variables private? Is it because they belong to the Member class and should only be accessed by Member objects?

            – Hristo
            Aug 5 '10 at 14:46






          • 3





            It is a best practice to make everything private that does not need to be accessed from outside, even in a small test project with only one class.

            – f1sh
            Aug 5 '10 at 14:48






          • 2





            @Hristo It's my belief that in general, every instance variable (also called fields, properties, and some other names) should be private. You want to expose as little of the internal workings of your class as possible. This helps you make your class more predictable because you won't have someone else modify the state of your class. 6 months down the road when you're fixing a bug in your code, you won't have to worry about 5 other files changing your variables if you keep them private.

            – Jonathon Faust
            Aug 5 '10 at 14:51






          • 1





            .. Thanks for response. That makes sense and it seems like its good practice to keep them private. Luckily, this isn't a long term project... its a short solution to a puzzle I'm working on, so it won't be going on a huge scale. But I'll keep this in mind for future projects :)

            – Hristo
            Aug 5 '10 at 14:57



















          • ohhh... I see. That makes a lot of sense. Why did you decide to make the 2 global variables private? Is it because they belong to the Member class and should only be accessed by Member objects?

            – Hristo
            Aug 5 '10 at 14:46






          • 3





            It is a best practice to make everything private that does not need to be accessed from outside, even in a small test project with only one class.

            – f1sh
            Aug 5 '10 at 14:48






          • 2





            @Hristo It's my belief that in general, every instance variable (also called fields, properties, and some other names) should be private. You want to expose as little of the internal workings of your class as possible. This helps you make your class more predictable because you won't have someone else modify the state of your class. 6 months down the road when you're fixing a bug in your code, you won't have to worry about 5 other files changing your variables if you keep them private.

            – Jonathon Faust
            Aug 5 '10 at 14:51






          • 1





            .. Thanks for response. That makes sense and it seems like its good practice to keep them private. Luckily, this isn't a long term project... its a short solution to a puzzle I'm working on, so it won't be going on a huge scale. But I'll keep this in mind for future projects :)

            – Hristo
            Aug 5 '10 at 14:57

















          ohhh... I see. That makes a lot of sense. Why did you decide to make the 2 global variables private? Is it because they belong to the Member class and should only be accessed by Member objects?

          – Hristo
          Aug 5 '10 at 14:46





          ohhh... I see. That makes a lot of sense. Why did you decide to make the 2 global variables private? Is it because they belong to the Member class and should only be accessed by Member objects?

          – Hristo
          Aug 5 '10 at 14:46




          3




          3





          It is a best practice to make everything private that does not need to be accessed from outside, even in a small test project with only one class.

          – f1sh
          Aug 5 '10 at 14:48





          It is a best practice to make everything private that does not need to be accessed from outside, even in a small test project with only one class.

          – f1sh
          Aug 5 '10 at 14:48




          2




          2





          @Hristo It's my belief that in general, every instance variable (also called fields, properties, and some other names) should be private. You want to expose as little of the internal workings of your class as possible. This helps you make your class more predictable because you won't have someone else modify the state of your class. 6 months down the road when you're fixing a bug in your code, you won't have to worry about 5 other files changing your variables if you keep them private.

          – Jonathon Faust
          Aug 5 '10 at 14:51





          @Hristo It's my belief that in general, every instance variable (also called fields, properties, and some other names) should be private. You want to expose as little of the internal workings of your class as possible. This helps you make your class more predictable because you won't have someone else modify the state of your class. 6 months down the road when you're fixing a bug in your code, you won't have to worry about 5 other files changing your variables if you keep them private.

          – Jonathon Faust
          Aug 5 '10 at 14:51




          1




          1





          .. Thanks for response. That makes sense and it seems like its good practice to keep them private. Luckily, this isn't a long term project... its a short solution to a puzzle I'm working on, so it won't be going on a huge scale. But I'll keep this in mind for future projects :)

          – Hristo
          Aug 5 '10 at 14:57





          .. Thanks for response. That makes sense and it seems like its good practice to keep them private. Luckily, this isn't a long term project... its a short solution to a puzzle I'm working on, so it won't be going on a huge scale. But I'll keep this in mind for future projects :)

          – Hristo
          Aug 5 '10 at 14:57













          4














          static variables are those that are shared across all objects of a class. Here in your example for every object of Member you create , you will get objects that have it's own iNumVertices values. When you use static with a variable, there is only one variable shared across every object of Member. static methods work the same way - they are shared across all objects.



          Since static variables/methods are common to all objects, one need not make an object of the class to access these variables/methods.



          Non-static variables like iNumVertices belong to an object of a class. They cannot be accessed without creating an object. So when you access a non-static variable from a static context (here main method), then java wouldn't know which object's iNumVertices you are trying to accesss. Hence the error.



          Either make iNumVertices static, or refer to it by creating an object of Member



          Member m = new Member();
          m.iNumVertices = 0;





          share|improve this answer
























          • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

            – Hristo
            Aug 5 '10 at 14:55











          • Exactly! That is exact purpose of member variables. You must not, however, confuse it with scope of a variable - public, private etc.

            – Ara
            Aug 5 '10 at 15:10













          • wait... I thought scope referred to the time range in which a variable lives. Are you saying that public, private, etc... also define the scope?

            – Hristo
            Aug 5 '10 at 15:22






          • 2





            public, private deals with inheritance and where all a variable/method is visible. static deals exclusively with sharing of variables within objects of a class. Scope is word used in both cases, but in completely different meanings.

            – Ara
            Aug 5 '10 at 15:31











          • interesting... I haven't encountered this before. I have always dealt with scope referring to the lifetime of a variable/object/etc... Thanks for your explanation.

            – Hristo
            Aug 5 '10 at 15:35
















          4














          static variables are those that are shared across all objects of a class. Here in your example for every object of Member you create , you will get objects that have it's own iNumVertices values. When you use static with a variable, there is only one variable shared across every object of Member. static methods work the same way - they are shared across all objects.



          Since static variables/methods are common to all objects, one need not make an object of the class to access these variables/methods.



          Non-static variables like iNumVertices belong to an object of a class. They cannot be accessed without creating an object. So when you access a non-static variable from a static context (here main method), then java wouldn't know which object's iNumVertices you are trying to accesss. Hence the error.



          Either make iNumVertices static, or refer to it by creating an object of Member



          Member m = new Member();
          m.iNumVertices = 0;





          share|improve this answer
























          • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

            – Hristo
            Aug 5 '10 at 14:55











          • Exactly! That is exact purpose of member variables. You must not, however, confuse it with scope of a variable - public, private etc.

            – Ara
            Aug 5 '10 at 15:10













          • wait... I thought scope referred to the time range in which a variable lives. Are you saying that public, private, etc... also define the scope?

            – Hristo
            Aug 5 '10 at 15:22






          • 2





            public, private deals with inheritance and where all a variable/method is visible. static deals exclusively with sharing of variables within objects of a class. Scope is word used in both cases, but in completely different meanings.

            – Ara
            Aug 5 '10 at 15:31











          • interesting... I haven't encountered this before. I have always dealt with scope referring to the lifetime of a variable/object/etc... Thanks for your explanation.

            – Hristo
            Aug 5 '10 at 15:35














          4












          4








          4







          static variables are those that are shared across all objects of a class. Here in your example for every object of Member you create , you will get objects that have it's own iNumVertices values. When you use static with a variable, there is only one variable shared across every object of Member. static methods work the same way - they are shared across all objects.



          Since static variables/methods are common to all objects, one need not make an object of the class to access these variables/methods.



          Non-static variables like iNumVertices belong to an object of a class. They cannot be accessed without creating an object. So when you access a non-static variable from a static context (here main method), then java wouldn't know which object's iNumVertices you are trying to accesss. Hence the error.



          Either make iNumVertices static, or refer to it by creating an object of Member



          Member m = new Member();
          m.iNumVertices = 0;





          share|improve this answer













          static variables are those that are shared across all objects of a class. Here in your example for every object of Member you create , you will get objects that have it's own iNumVertices values. When you use static with a variable, there is only one variable shared across every object of Member. static methods work the same way - they are shared across all objects.



          Since static variables/methods are common to all objects, one need not make an object of the class to access these variables/methods.



          Non-static variables like iNumVertices belong to an object of a class. They cannot be accessed without creating an object. So when you access a non-static variable from a static context (here main method), then java wouldn't know which object's iNumVertices you are trying to accesss. Hence the error.



          Either make iNumVertices static, or refer to it by creating an object of Member



          Member m = new Member();
          m.iNumVertices = 0;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 5 '10 at 14:50









          AraAra

          1036




          1036













          • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

            – Hristo
            Aug 5 '10 at 14:55











          • Exactly! That is exact purpose of member variables. You must not, however, confuse it with scope of a variable - public, private etc.

            – Ara
            Aug 5 '10 at 15:10













          • wait... I thought scope referred to the time range in which a variable lives. Are you saying that public, private, etc... also define the scope?

            – Hristo
            Aug 5 '10 at 15:22






          • 2





            public, private deals with inheritance and where all a variable/method is visible. static deals exclusively with sharing of variables within objects of a class. Scope is word used in both cases, but in completely different meanings.

            – Ara
            Aug 5 '10 at 15:31











          • interesting... I haven't encountered this before. I have always dealt with scope referring to the lifetime of a variable/object/etc... Thanks for your explanation.

            – Hristo
            Aug 5 '10 at 15:35



















          • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

            – Hristo
            Aug 5 '10 at 14:55











          • Exactly! That is exact purpose of member variables. You must not, however, confuse it with scope of a variable - public, private etc.

            – Ara
            Aug 5 '10 at 15:10













          • wait... I thought scope referred to the time range in which a variable lives. Are you saying that public, private, etc... also define the scope?

            – Hristo
            Aug 5 '10 at 15:22






          • 2





            public, private deals with inheritance and where all a variable/method is visible. static deals exclusively with sharing of variables within objects of a class. Scope is word used in both cases, but in completely different meanings.

            – Ara
            Aug 5 '10 at 15:31











          • interesting... I haven't encountered this before. I have always dealt with scope referring to the lifetime of a variable/object/etc... Thanks for your explanation.

            – Hristo
            Aug 5 '10 at 15:35

















          So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

          – Hristo
          Aug 5 '10 at 14:55





          So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

          – Hristo
          Aug 5 '10 at 14:55













          Exactly! That is exact purpose of member variables. You must not, however, confuse it with scope of a variable - public, private etc.

          – Ara
          Aug 5 '10 at 15:10







          Exactly! That is exact purpose of member variables. You must not, however, confuse it with scope of a variable - public, private etc.

          – Ara
          Aug 5 '10 at 15:10















          wait... I thought scope referred to the time range in which a variable lives. Are you saying that public, private, etc... also define the scope?

          – Hristo
          Aug 5 '10 at 15:22





          wait... I thought scope referred to the time range in which a variable lives. Are you saying that public, private, etc... also define the scope?

          – Hristo
          Aug 5 '10 at 15:22




          2




          2





          public, private deals with inheritance and where all a variable/method is visible. static deals exclusively with sharing of variables within objects of a class. Scope is word used in both cases, but in completely different meanings.

          – Ara
          Aug 5 '10 at 15:31





          public, private deals with inheritance and where all a variable/method is visible. static deals exclusively with sharing of variables within objects of a class. Scope is word used in both cases, but in completely different meanings.

          – Ara
          Aug 5 '10 at 15:31













          interesting... I haven't encountered this before. I have always dealt with scope referring to the lifetime of a variable/object/etc... Thanks for your explanation.

          – Hristo
          Aug 5 '10 at 15:35





          interesting... I haven't encountered this before. I have always dealt with scope referring to the lifetime of a variable/object/etc... Thanks for your explanation.

          – Hristo
          Aug 5 '10 at 15:35











          4














          learn what "static" actually means


          What static actually means that Class variable will be same for all instance of that particular class, however if you want to avoid using static variables(which is a good idea, since static variables are being kept in memory) you can pass variable value trough constructor thereby avoiding usage of static modifier, and achieve the same effect(that is if you pass the same value upon class instantiation).



          Here is code example :



          public class Car{

          private int speed;

          public Car(int speed){
          this.speed = speed;
          }

          }


          So you can do this when creating new instance :



          Car car = new Car(100);


          and every time you create Car instance it will have speed 100, thus avoiding static declaration private static int speed = 100;






          share|improve this answer


























          • ahh that's interesting... slightly different solution from the other answers. mind posting some code to demonstrate?

            – Hristo
            Aug 5 '10 at 15:29











          • @Hristo I added some code

            – Gandalf StormCrow
            Aug 5 '10 at 15:36











          • Thanks for the code.

            – Hristo
            Aug 5 '10 at 15:58






          • 1





            @Hristo if you want your example to work, you need to create function which sets the value of iNumVertices to 0(so called setter function). Then inside main you should instantiate your class Member member = new Member();, then call setter function to change the value of your class field, member.setINumVertices(0);

            – Gandalf StormCrow
            Aug 5 '10 at 16:06











          • .. ahh right. That makes total sense. Thanks!

            – Hristo
            Aug 5 '10 at 16:13
















          4














          learn what "static" actually means


          What static actually means that Class variable will be same for all instance of that particular class, however if you want to avoid using static variables(which is a good idea, since static variables are being kept in memory) you can pass variable value trough constructor thereby avoiding usage of static modifier, and achieve the same effect(that is if you pass the same value upon class instantiation).



          Here is code example :



          public class Car{

          private int speed;

          public Car(int speed){
          this.speed = speed;
          }

          }


          So you can do this when creating new instance :



          Car car = new Car(100);


          and every time you create Car instance it will have speed 100, thus avoiding static declaration private static int speed = 100;






          share|improve this answer


























          • ahh that's interesting... slightly different solution from the other answers. mind posting some code to demonstrate?

            – Hristo
            Aug 5 '10 at 15:29











          • @Hristo I added some code

            – Gandalf StormCrow
            Aug 5 '10 at 15:36











          • Thanks for the code.

            – Hristo
            Aug 5 '10 at 15:58






          • 1





            @Hristo if you want your example to work, you need to create function which sets the value of iNumVertices to 0(so called setter function). Then inside main you should instantiate your class Member member = new Member();, then call setter function to change the value of your class field, member.setINumVertices(0);

            – Gandalf StormCrow
            Aug 5 '10 at 16:06











          • .. ahh right. That makes total sense. Thanks!

            – Hristo
            Aug 5 '10 at 16:13














          4












          4








          4







          learn what "static" actually means


          What static actually means that Class variable will be same for all instance of that particular class, however if you want to avoid using static variables(which is a good idea, since static variables are being kept in memory) you can pass variable value trough constructor thereby avoiding usage of static modifier, and achieve the same effect(that is if you pass the same value upon class instantiation).



          Here is code example :



          public class Car{

          private int speed;

          public Car(int speed){
          this.speed = speed;
          }

          }


          So you can do this when creating new instance :



          Car car = new Car(100);


          and every time you create Car instance it will have speed 100, thus avoiding static declaration private static int speed = 100;






          share|improve this answer















          learn what "static" actually means


          What static actually means that Class variable will be same for all instance of that particular class, however if you want to avoid using static variables(which is a good idea, since static variables are being kept in memory) you can pass variable value trough constructor thereby avoiding usage of static modifier, and achieve the same effect(that is if you pass the same value upon class instantiation).



          Here is code example :



          public class Car{

          private int speed;

          public Car(int speed){
          this.speed = speed;
          }

          }


          So you can do this when creating new instance :



          Car car = new Car(100);


          and every time you create Car instance it will have speed 100, thus avoiding static declaration private static int speed = 100;







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 5 '10 at 17:29









          Chadwick

          10.4k74164




          10.4k74164










          answered Aug 5 '10 at 15:24









          Gandalf StormCrowGandalf StormCrow

          9,59158143232




          9,59158143232













          • ahh that's interesting... slightly different solution from the other answers. mind posting some code to demonstrate?

            – Hristo
            Aug 5 '10 at 15:29











          • @Hristo I added some code

            – Gandalf StormCrow
            Aug 5 '10 at 15:36











          • Thanks for the code.

            – Hristo
            Aug 5 '10 at 15:58






          • 1





            @Hristo if you want your example to work, you need to create function which sets the value of iNumVertices to 0(so called setter function). Then inside main you should instantiate your class Member member = new Member();, then call setter function to change the value of your class field, member.setINumVertices(0);

            – Gandalf StormCrow
            Aug 5 '10 at 16:06











          • .. ahh right. That makes total sense. Thanks!

            – Hristo
            Aug 5 '10 at 16:13



















          • ahh that's interesting... slightly different solution from the other answers. mind posting some code to demonstrate?

            – Hristo
            Aug 5 '10 at 15:29











          • @Hristo I added some code

            – Gandalf StormCrow
            Aug 5 '10 at 15:36











          • Thanks for the code.

            – Hristo
            Aug 5 '10 at 15:58






          • 1





            @Hristo if you want your example to work, you need to create function which sets the value of iNumVertices to 0(so called setter function). Then inside main you should instantiate your class Member member = new Member();, then call setter function to change the value of your class field, member.setINumVertices(0);

            – Gandalf StormCrow
            Aug 5 '10 at 16:06











          • .. ahh right. That makes total sense. Thanks!

            – Hristo
            Aug 5 '10 at 16:13

















          ahh that's interesting... slightly different solution from the other answers. mind posting some code to demonstrate?

          – Hristo
          Aug 5 '10 at 15:29





          ahh that's interesting... slightly different solution from the other answers. mind posting some code to demonstrate?

          – Hristo
          Aug 5 '10 at 15:29













          @Hristo I added some code

          – Gandalf StormCrow
          Aug 5 '10 at 15:36





          @Hristo I added some code

          – Gandalf StormCrow
          Aug 5 '10 at 15:36













          Thanks for the code.

          – Hristo
          Aug 5 '10 at 15:58





          Thanks for the code.

          – Hristo
          Aug 5 '10 at 15:58




          1




          1





          @Hristo if you want your example to work, you need to create function which sets the value of iNumVertices to 0(so called setter function). Then inside main you should instantiate your class Member member = new Member();, then call setter function to change the value of your class field, member.setINumVertices(0);

          – Gandalf StormCrow
          Aug 5 '10 at 16:06





          @Hristo if you want your example to work, you need to create function which sets the value of iNumVertices to 0(so called setter function). Then inside main you should instantiate your class Member member = new Member();, then call setter function to change the value of your class field, member.setINumVertices(0);

          – Gandalf StormCrow
          Aug 5 '10 at 16:06













          .. ahh right. That makes total sense. Thanks!

          – Hristo
          Aug 5 '10 at 16:13





          .. ahh right. That makes total sense. Thanks!

          – Hristo
          Aug 5 '10 at 16:13











          2














          Static variables are class variables. There will be a single copy of that variable avaiable to all instances of the class and they will share that variable. Static members can also be used without referencing a specific instance of the class.



          More here:



          http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html






          share|improve this answer




























            2














            Static variables are class variables. There will be a single copy of that variable avaiable to all instances of the class and they will share that variable. Static members can also be used without referencing a specific instance of the class.



            More here:



            http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html






            share|improve this answer


























              2












              2








              2







              Static variables are class variables. There will be a single copy of that variable avaiable to all instances of the class and they will share that variable. Static members can also be used without referencing a specific instance of the class.



              More here:



              http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html






              share|improve this answer













              Static variables are class variables. There will be a single copy of that variable avaiable to all instances of the class and they will share that variable. Static members can also be used without referencing a specific instance of the class.



              More here:



              http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 5 '10 at 14:37









              FrustratedWithFormsDesignerFrustratedWithFormsDesigner

              20.9k26115173




              20.9k26115173























                  1














                  Static variables do not need to have the class be instantiated in order to be accessed, so if you are trying to access a non-static variable form a static context you are risking trying to access something that has not been initialized/instantiated.






                  share|improve this answer




























                    1














                    Static variables do not need to have the class be instantiated in order to be accessed, so if you are trying to access a non-static variable form a static context you are risking trying to access something that has not been initialized/instantiated.






                    share|improve this answer


























                      1












                      1








                      1







                      Static variables do not need to have the class be instantiated in order to be accessed, so if you are trying to access a non-static variable form a static context you are risking trying to access something that has not been initialized/instantiated.






                      share|improve this answer













                      Static variables do not need to have the class be instantiated in order to be accessed, so if you are trying to access a non-static variable form a static context you are risking trying to access something that has not been initialized/instantiated.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 5 '10 at 14:39









                      davedave

                      8,39753557




                      8,39753557























                          1














                          Static methods can access only static variables. There are two kinds of variables in class. one is static variables(also class variables) and other is instance variable. Only one copy of Static variable exists in memory but instance variables will be instantiated for each object. So for static variables all objects access the same variable and any change made by one object will be reflected to other objects.
                          The question here is why is that methods have to be static to access static variables. When you make a method static, you can access the method without instantiating objects of that class. So if this method is able to access instance variables then for which object's variables should it make change to? The other way is possible ie non static methods can access static variables.






                          share|improve this answer
























                          • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

                            – Hristo
                            Aug 5 '10 at 14:58






                          • 1





                            @Hristo Correct. The term "instance" refers to a unique, individual realization of the class. The class describes how each instance should be structured. Statics sidestep that and aren't really part of the individual instances. They can't be unique to an instance -- there's only one copy of a static, not one associated with each instance.

                            – Jonathon Faust
                            Aug 5 '10 at 15:05











                          • Thanks for the explanation. That makes sense.

                            – Hristo
                            Aug 5 '10 at 15:21
















                          1














                          Static methods can access only static variables. There are two kinds of variables in class. one is static variables(also class variables) and other is instance variable. Only one copy of Static variable exists in memory but instance variables will be instantiated for each object. So for static variables all objects access the same variable and any change made by one object will be reflected to other objects.
                          The question here is why is that methods have to be static to access static variables. When you make a method static, you can access the method without instantiating objects of that class. So if this method is able to access instance variables then for which object's variables should it make change to? The other way is possible ie non static methods can access static variables.






                          share|improve this answer
























                          • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

                            – Hristo
                            Aug 5 '10 at 14:58






                          • 1





                            @Hristo Correct. The term "instance" refers to a unique, individual realization of the class. The class describes how each instance should be structured. Statics sidestep that and aren't really part of the individual instances. They can't be unique to an instance -- there's only one copy of a static, not one associated with each instance.

                            – Jonathon Faust
                            Aug 5 '10 at 15:05











                          • Thanks for the explanation. That makes sense.

                            – Hristo
                            Aug 5 '10 at 15:21














                          1












                          1








                          1







                          Static methods can access only static variables. There are two kinds of variables in class. one is static variables(also class variables) and other is instance variable. Only one copy of Static variable exists in memory but instance variables will be instantiated for each object. So for static variables all objects access the same variable and any change made by one object will be reflected to other objects.
                          The question here is why is that methods have to be static to access static variables. When you make a method static, you can access the method without instantiating objects of that class. So if this method is able to access instance variables then for which object's variables should it make change to? The other way is possible ie non static methods can access static variables.






                          share|improve this answer













                          Static methods can access only static variables. There are two kinds of variables in class. one is static variables(also class variables) and other is instance variable. Only one copy of Static variable exists in memory but instance variables will be instantiated for each object. So for static variables all objects access the same variable and any change made by one object will be reflected to other objects.
                          The question here is why is that methods have to be static to access static variables. When you make a method static, you can access the method without instantiating objects of that class. So if this method is able to access instance variables then for which object's variables should it make change to? The other way is possible ie non static methods can access static variables.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 5 '10 at 14:48









                          user281693user281693

                          3762516




                          3762516













                          • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

                            – Hristo
                            Aug 5 '10 at 14:58






                          • 1





                            @Hristo Correct. The term "instance" refers to a unique, individual realization of the class. The class describes how each instance should be structured. Statics sidestep that and aren't really part of the individual instances. They can't be unique to an instance -- there's only one copy of a static, not one associated with each instance.

                            – Jonathon Faust
                            Aug 5 '10 at 15:05











                          • Thanks for the explanation. That makes sense.

                            – Hristo
                            Aug 5 '10 at 15:21



















                          • So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

                            – Hristo
                            Aug 5 '10 at 14:58






                          • 1





                            @Hristo Correct. The term "instance" refers to a unique, individual realization of the class. The class describes how each instance should be structured. Statics sidestep that and aren't really part of the individual instances. They can't be unique to an instance -- there's only one copy of a static, not one associated with each instance.

                            – Jonathon Faust
                            Aug 5 '10 at 15:05











                          • Thanks for the explanation. That makes sense.

                            – Hristo
                            Aug 5 '10 at 15:21

















                          So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

                          – Hristo
                          Aug 5 '10 at 14:58





                          So if I want a variable to be specific to each object... if I want each Member to be independent of another Member (for example if they are different ages), then I shouldn't be using static right?

                          – Hristo
                          Aug 5 '10 at 14:58




                          1




                          1





                          @Hristo Correct. The term "instance" refers to a unique, individual realization of the class. The class describes how each instance should be structured. Statics sidestep that and aren't really part of the individual instances. They can't be unique to an instance -- there's only one copy of a static, not one associated with each instance.

                          – Jonathon Faust
                          Aug 5 '10 at 15:05





                          @Hristo Correct. The term "instance" refers to a unique, individual realization of the class. The class describes how each instance should be structured. Statics sidestep that and aren't really part of the individual instances. They can't be unique to an instance -- there's only one copy of a static, not one associated with each instance.

                          – Jonathon Faust
                          Aug 5 '10 at 15:05













                          Thanks for the explanation. That makes sense.

                          – Hristo
                          Aug 5 '10 at 15:21





                          Thanks for the explanation. That makes sense.

                          – Hristo
                          Aug 5 '10 at 15:21











                          0














                          Every class variable (a variable declared within the class body and outside the method bodies) used in a static method needs to be declared static too.



                          Static class variables and methods can be accessed outside that class without the need for an instance of that class.






                          share|improve this answer




























                            0














                            Every class variable (a variable declared within the class body and outside the method bodies) used in a static method needs to be declared static too.



                            Static class variables and methods can be accessed outside that class without the need for an instance of that class.






                            share|improve this answer


























                              0












                              0








                              0







                              Every class variable (a variable declared within the class body and outside the method bodies) used in a static method needs to be declared static too.



                              Static class variables and methods can be accessed outside that class without the need for an instance of that class.






                              share|improve this answer













                              Every class variable (a variable declared within the class body and outside the method bodies) used in a static method needs to be declared static too.



                              Static class variables and methods can be accessed outside that class without the need for an instance of that class.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 5 '10 at 14:41









                              MolskeMolske

                              2,59713042




                              2,59713042























                                  0














                                  static variables are common to all instances of a Class.



                                  Note: As said earlier these are class variables i.e. shared by all instances.



                                  These can also be called as class level variables.
                                  Generally you define Constants(You will also need final keyword for defining constants) and Global variables as static.



                                  For more information refer:
                                  http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html



                                  http://www.roseindia.net/java/beginners/staticvariable.shtml






                                  share|improve this answer






























                                    0














                                    static variables are common to all instances of a Class.



                                    Note: As said earlier these are class variables i.e. shared by all instances.



                                    These can also be called as class level variables.
                                    Generally you define Constants(You will also need final keyword for defining constants) and Global variables as static.



                                    For more information refer:
                                    http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html



                                    http://www.roseindia.net/java/beginners/staticvariable.shtml






                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      static variables are common to all instances of a Class.



                                      Note: As said earlier these are class variables i.e. shared by all instances.



                                      These can also be called as class level variables.
                                      Generally you define Constants(You will also need final keyword for defining constants) and Global variables as static.



                                      For more information refer:
                                      http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html



                                      http://www.roseindia.net/java/beginners/staticvariable.shtml






                                      share|improve this answer















                                      static variables are common to all instances of a Class.



                                      Note: As said earlier these are class variables i.e. shared by all instances.



                                      These can also be called as class level variables.
                                      Generally you define Constants(You will also need final keyword for defining constants) and Global variables as static.



                                      For more information refer:
                                      http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html



                                      http://www.roseindia.net/java/beginners/staticvariable.shtml







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Aug 5 '10 at 14:47

























                                      answered Aug 5 '10 at 14:41









                                      YoKYoK

                                      12.3k44266




                                      12.3k44266






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3415781%2fwhat-exactly-does-static-mean-when-declaring-global-variables-in-java%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Contact image not getting when fetch all contact list from iPhone by CNContact

                                          count number of partitions of a set with n elements into k subsets

                                          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks