Non-static variable cannot be referenced from a static context












234















I've written this test code:



class MyProgram
{
int count = 0;
public static void main(String args)
{
System.out.println(count);
}
}


But it gives the following error:



Main.java:6: error: non-static variable count cannot be referenced from a static context
System.out.println(count);
^


How do I get my methods to recognize my class variables?










share|improve this question

























  • This might help - buggybread.com/2014/06/…

    – Vivek Vermani
    Aug 25 '14 at 21:04











  • Try to avoid using static whenever possible. You can write a complete program, all static, just like in C. But it won't be a very good one. Try to use Java the way it is meant to be used, as an object oriented language.

    – Erick G. Hagstrom
    Sep 15 '15 at 16:21
















234















I've written this test code:



class MyProgram
{
int count = 0;
public static void main(String args)
{
System.out.println(count);
}
}


But it gives the following error:



Main.java:6: error: non-static variable count cannot be referenced from a static context
System.out.println(count);
^


How do I get my methods to recognize my class variables?










share|improve this question

























  • This might help - buggybread.com/2014/06/…

    – Vivek Vermani
    Aug 25 '14 at 21:04











  • Try to avoid using static whenever possible. You can write a complete program, all static, just like in C. But it won't be a very good one. Try to use Java the way it is meant to be used, as an object oriented language.

    – Erick G. Hagstrom
    Sep 15 '15 at 16:21














234












234








234


107






I've written this test code:



class MyProgram
{
int count = 0;
public static void main(String args)
{
System.out.println(count);
}
}


But it gives the following error:



Main.java:6: error: non-static variable count cannot be referenced from a static context
System.out.println(count);
^


How do I get my methods to recognize my class variables?










share|improve this question
















I've written this test code:



class MyProgram
{
int count = 0;
public static void main(String args)
{
System.out.println(count);
}
}


But it gives the following error:



Main.java:6: error: non-static variable count cannot be referenced from a static context
System.out.println(count);
^


How do I get my methods to recognize my class variables?







java variables compiler-errors non-static






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 15 '17 at 20:14









Dukeling

44.7k1060105




44.7k1060105










asked Apr 1 '10 at 9:57









GregGreg

1,180383




1,180383













  • This might help - buggybread.com/2014/06/…

    – Vivek Vermani
    Aug 25 '14 at 21:04











  • Try to avoid using static whenever possible. You can write a complete program, all static, just like in C. But it won't be a very good one. Try to use Java the way it is meant to be used, as an object oriented language.

    – Erick G. Hagstrom
    Sep 15 '15 at 16:21



















  • This might help - buggybread.com/2014/06/…

    – Vivek Vermani
    Aug 25 '14 at 21:04











  • Try to avoid using static whenever possible. You can write a complete program, all static, just like in C. But it won't be a very good one. Try to use Java the way it is meant to be used, as an object oriented language.

    – Erick G. Hagstrom
    Sep 15 '15 at 16:21

















This might help - buggybread.com/2014/06/…

– Vivek Vermani
Aug 25 '14 at 21:04





This might help - buggybread.com/2014/06/…

– Vivek Vermani
Aug 25 '14 at 21:04













Try to avoid using static whenever possible. You can write a complete program, all static, just like in C. But it won't be a very good one. Try to use Java the way it is meant to be used, as an object oriented language.

– Erick G. Hagstrom
Sep 15 '15 at 16:21





Try to avoid using static whenever possible. You can write a complete program, all static, just like in C. But it won't be a very good one. Try to use Java the way it is meant to be used, as an object oriented language.

– Erick G. Hagstrom
Sep 15 '15 at 16:21












12 Answers
12






active

oldest

votes


















240














You must understand the difference between a class and an instance of that class. If you see a car on the street, you know immediately that it's a car even if you can't see which model or type. This is because you compare what you see with the class "car". The class contains which is similar to all cars. Think of it as a template or an idea.



At the same time, the car you see is an instance of the class "car" since it has all the properties which you expect: There is someone driving it, it has an engine, wheels.



So the class says "all cars have a color" and the instance says "this specific car is red".



In the OO world, you define the class and inside the class, you define a field of type Color. When the class is instantiated (when you create a specific instance), memory is reserved for the color and you can give this specific instance a color. Since these attributes are specific, they are non-static.



Static fields and methods are shared with all instances. They are for values which are specific to the class and not a specific instance. For methods, this usually are global helper methods (like Integer.parseInt()). For fields, it's usually constants (like car types, i.e. something where you have a limited set which doesn't change often).



To solve your problem, you need to instantiate an instance (create an object) of your class so the runtime can reserve memory for the instance (otherwise, different instances would overwrite each other which you don't want).



In your case, try this code as a starting block:



public static void main (String args)
{
try
{
MyProgram7 obj = new MyProgram7 ();
obj.run (args);
}
catch (Exception e)
{
e.printStackTrace ();
}
}

// instance variables here

public void run (String args) throws Exception
{
// put your code here
}


The new main() method creates an instance of the class it contains (sounds strange but since main() is created with the class instead of with the instance, it can do this) and then calls an instance method (run()).






share|improve this answer
























  • I am explaining this at the moment to our new colleague - thanks for this great explanation. This should the acceptet answer.

    – Supahupe
    Jan 9 '18 at 13:45





















67














Static fields and methods are connected to the class itself and not its instances. If you have a class A, a 'normal' method b, and a static method c, and you make an instance a of your class A, the calls to A.c() and a.b() are valid. Method c() has no idea which instance is connected, so it cannot use non-static fields.



The solution for you is that you either make your fields static or your methods non-static. You main could look like this then:



class Programm {
public static void main(String args){
Programm programm = new Programm();
programm.start();
}
public void start(){
// can now access non-static fields
}
}





share|improve this answer

































    30














    The static keyword modifies the lifecycle of a method or variable within a class. A static method or variable is created at the time a class is loaded. A method or variable that is not declared as static is created only when the class is instantiated as an object for example by using the new operator.



    The lifecycle of a class, in broad terms, is:




    1. the source code for the class is written creating a template or
      pattern or stamp which can then be used to

    2. create an object with the new operator using the class to make an instance of the class as an actual object and then when done with the object

    3. destroy the object reclaiming the resources it is holding such as memory during garbage collection.


    In order to have an initial entry point for an application, Java has adopted the convention that the Java program must have a class that contains a method with an agreed upon or special name. This special method is called main(). Since the method must exist whether the class containing the main method has been instantiated or not, the main() method must be declared with the static modifier so that as soon as the class is loaded, the main() method is available.



    The result is that when you start your Java application by a command line such as java helloworld a series of actions happen. First of all a Java Virtual Machine is started up and initialized. Next the helloworld.class file containing the compiled Java code is loaded into the Java Virtual Machine. Then the Java Virtual Machine looks for a method in the helloworld class that is called main(String args). this method must be static so that it will exist even though the class has not actually been instantiated as an object. The Java Virtual Machine does not create an instance of the class by creating an object from the class. It just loads the class and starts execution at the main() method.



    So you need to create an instance of your class as an object and then you can access the methods and variables of the class that have not been declared with the static modifier. Once your Java program has started with the main() function you can then use any variables or methods that have the modifier of static since they exist as part of the class being loaded.



    However, those variables and methods of the class which are outside of the main() method which do not have the static modifier can not be used until an instance of the class has been created as an object within the main() method. After creating the object you can then use the variables and methods of the object. An attempt to use the variables and methods of the class which do not have the static modifier without going through an object of the class is caught by the Java compiler at compile time and flagged as an error.



    import java.io.*;

    class helloworld {
    int myInt; // this is a class variable that is unique to each object
    static int myInt2; // this is a class variable shared by all objects of this class

    static void main (String args) {
    // this is the main entry point for this Java application
    System.out.println ("Hello, Worldn");
    myInt2 = 14; // able to access the static int
    helloworld myWorld = new helloworld();
    myWorld.myInt = 32; // able to access non-static through an object
    }
    }





    share|improve this answer

































      11














      Let's analyze your program first..
      In your program, your first method is main(), and keep it in mind it is the static method... Then you declare the local variable for that method (compareCount, low, high, etc..). The scope of this variable is only the declared method, regardless of it being a static or non static method. So you can't use those variables outside that method. This is the basic error u made.



      Then we come to next point. You told static is killing you. (It may be killing you but it only gives life to your program!!) First you must understand the basic thing.
      *Static method calls only the static method and use only the static variable.
      *Static variable or static method are not dependent on any instance of that class. (i.e. If you change any state of the static variable it will reflect in all objects of the class)
      *Because of this you call it as a class variable or a class method.
      And a lot more is there about the "static" keyword.
      I hope now you get the idea. First change the scope of the variable and declare it as a static (to be able to use it in static methods).



      And the advice for you is: you misunderstood the idea of the scope of the variables and static functionalities. Get clear idea about that.






      share|improve this answer

































        9














        The very basic thing is static variables or static methods are at class level. Class level variables or methods gets loaded prior to instance level methods or variables.And obviously the thing which is not loaded can not be used. So java compiler not letting the things to be handled at run time resolves at compile time. That's why it is giving you error non-static things can not be referred from static context. You just need to read about Class Level Scope, Instance Level Scope and Local Scope.






        share|improve this answer































          8














          To be able to access them from your static methods they need to be static member variables, like this:



          public class MyProgram7 {
          static Scanner scan = new Scanner(System.in);
          static int compareCount = 0;
          static int low = 0;
          static int high = 0;
          static int mid = 0;
          static int key = 0;
          static Scanner temp;
          static intlist;
          static String menu, outputString;
          static int option = 1;
          static boolean found = false;

          public static void main (Stringargs) throws IOException {
          ...





          share|improve this answer































            7














            Now you can add/use instances with in the method



            public class Myprogram7 {

            Scanner scan;
            int compareCount = 0;
            int low = 0;
            int high = 0;
            int mid = 0;
            int key = 0;
            Scanner temp;
            intlist;
            String menu, outputString;
            int option = 1;
            boolean found = false;

            private void readLine() {

            }

            private void findkey() {

            }

            private void printCount() {

            }
            public static void main(String args){

            Myprogram7 myprg=new Myprogram7();
            myprg.readLine();
            myprg.findkey();
            myprg.printCount();
            }
            }





            share|improve this answer


























            • Very solid example that I used as a template to revised a complex src file into a proper structure.

              – XO.
              Nov 27 '18 at 4:49



















            4














            I will try to explain the static thing to you. First of all static variables do not belong to any particular instance of the class. They are recognized with the name of the class. Static methods again do not belong again to any particular instance. They can access only static variables. Imagine you call MyClass.myMethod() and myMethod is a static method. If you use non-static variables inside the method, how the hell on earth would it know which variables to use? That's why you can use from static methods only static variables. I repeat again they do NOT belong to any particular instance.






            share|improve this answer































              2















              • The first thing is to know the difference between an instance of a class, and the class itself. A class models certain properties, and the behaviour of the whole in the context of those properties. An instance will define specific values for those properties.


              • Anything bound to the static keyword is available in the context of the class rather than in the context of an instance of the class



              • As a corollary to the above




                1. variables within a method can not be static

                2. static fields, and methods must be invoked using the class-name e.g. MyProgram7.main(...)



              • The lifetime of a static field/method is equivalent to the lifetime of your application



              E.g.
              Say, car has the property colour, and exhibits the behaviour 'motion'.
              An instance of the car would be a Red Volkswagen Beetle in motion at 25kmph.



              Now a static property of the car would be the number of wheels (4) on the road, and this would apply to all cars.



              HTH






              share|improve this answer































                1














                This is bit diff to explain about static key word for all beginners.

                You wil get to know it clearly when you work more with Classes and Objects.



                |*| Static : Static items can be called with Class Name

                If you observe in codes, Some functions are directly called with Class names like



                NamCls.NamFnc();

                System.out.println();


                This is because NamFnc and println wil be declared using key word static before them.



                |*| Non Static :Non Static items can be called with Class Variable

                If its not static, you need a variable of the class,

                put dot after the class variable and

                then call function.



                NamCls NamObjVar = new NamCls();
                NamObjVar.NamFnc();





                Below code explains you neatly

                |*| Static and non Static function in class :



                public class NamCls
                {
                public static void main(String args)
                {
                PlsPrnFnc("Tst Txt");

                NamCls NamObjVar = new NamCls();
                NamObjVar.PrnFnc("Tst Txt");
                }

                static void PlsPrnFnc(String SrgPsgVal)
                {
                System.out.println(SrgPsgVal);
                }

                void PrnFnc(String SrgPsgVal)
                {
                System.out.println(SrgPsgVal);
                }
                }





                |*| Static and non Static Class inside a Class :

                public class NamCls
                {
                public static void main(String args)
                {
                NamTicCls NamTicVaj = new NamTicCls();
                NamTicVaj.PrnFnc("Tst Txt");

                NamCls NamObjVar = new NamCls();
                NamNicCls NamNicVar = NamObjVar.new NamNicCls();
                NamNicVar.PrnFnc("Tst Txt");
                }

                static class NamTicCls
                {
                void PrnFnc(String SrgPsgVal)
                {
                System.out.println(SrgPsgVal);
                }
                }

                class NamNicCls
                {
                void PrnFnc(String SrgPsgVal)
                {
                System.out.println(SrgPsgVal);
                }
                }
                }





                share|improve this answer































                  1














                  Before you call an instance method or instance variable It needs a object(Instance). When instance variable is called from static method compiler doesn't know which is the object this variable belongs to. Because static methods doesn't have an object (Only one copy always). When you call an instance variable or instance methods from instance method it refer the this object. It means the variable belongs to whatever object created and each object have it's own copy of instance methods and variables.



                  Static variables are marked as static and instance variables doesn't have specific keyword.






                  share|improve this answer































                    0














                    It is ClassLoader responsible to load the class files.Let's see what happens when we write our own classes.



                    Example 1:



                    class StaticTest {

                    static int a;
                    int b;
                    int c;
                    }


                    Now we can see that class "StaticTest" has 3 fields.But actually there is no existence of b,c member variable.But why ???. OK Lest's see. Here b,c are instance variable.Since instance variable gets the memory at the time of object creation. So here b,c are not getting any memory yet. That's why there is no existence of b,c. So There is only existence of a.
                    For ClassLoader it has only one information about a. ClassLoader yet not recognize b,c because it's object not instantiated yet.



                    Let's see another example:
                    Example 2:



                    class StaticTest {

                    public void display() {
                    System.out.println("Static Test");
                    }


                    public static void main(String cmd) {

                    display();
                    }

                    }


                    Now if we try to compile this code compiler will give CE error.
                    CE: non-static method display() cannot be referenced from a static context.



                    Now For ClassLoader it looks like:



                    class StaticTest {

                    public static void main(String cmd) {

                    display();
                    }

                    }


                    In Example 2 CE error is because we call non static method from a static context. So it is not possible for ClassLoader to recognize method display() at compile time.So compile time error is occurred.






                    share|improve this answer


























                    • Probably submitted your answer by accident before you managed to finish it? Please edit it and add the missing content, thanks!

                      – plamut
                      Sep 3 '15 at 12:27



















                    12 Answers
                    12






                    active

                    oldest

                    votes








                    12 Answers
                    12






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    240














                    You must understand the difference between a class and an instance of that class. If you see a car on the street, you know immediately that it's a car even if you can't see which model or type. This is because you compare what you see with the class "car". The class contains which is similar to all cars. Think of it as a template or an idea.



                    At the same time, the car you see is an instance of the class "car" since it has all the properties which you expect: There is someone driving it, it has an engine, wheels.



                    So the class says "all cars have a color" and the instance says "this specific car is red".



                    In the OO world, you define the class and inside the class, you define a field of type Color. When the class is instantiated (when you create a specific instance), memory is reserved for the color and you can give this specific instance a color. Since these attributes are specific, they are non-static.



                    Static fields and methods are shared with all instances. They are for values which are specific to the class and not a specific instance. For methods, this usually are global helper methods (like Integer.parseInt()). For fields, it's usually constants (like car types, i.e. something where you have a limited set which doesn't change often).



                    To solve your problem, you need to instantiate an instance (create an object) of your class so the runtime can reserve memory for the instance (otherwise, different instances would overwrite each other which you don't want).



                    In your case, try this code as a starting block:



                    public static void main (String args)
                    {
                    try
                    {
                    MyProgram7 obj = new MyProgram7 ();
                    obj.run (args);
                    }
                    catch (Exception e)
                    {
                    e.printStackTrace ();
                    }
                    }

                    // instance variables here

                    public void run (String args) throws Exception
                    {
                    // put your code here
                    }


                    The new main() method creates an instance of the class it contains (sounds strange but since main() is created with the class instead of with the instance, it can do this) and then calls an instance method (run()).






                    share|improve this answer
























                    • I am explaining this at the moment to our new colleague - thanks for this great explanation. This should the acceptet answer.

                      – Supahupe
                      Jan 9 '18 at 13:45


















                    240














                    You must understand the difference between a class and an instance of that class. If you see a car on the street, you know immediately that it's a car even if you can't see which model or type. This is because you compare what you see with the class "car". The class contains which is similar to all cars. Think of it as a template or an idea.



                    At the same time, the car you see is an instance of the class "car" since it has all the properties which you expect: There is someone driving it, it has an engine, wheels.



                    So the class says "all cars have a color" and the instance says "this specific car is red".



                    In the OO world, you define the class and inside the class, you define a field of type Color. When the class is instantiated (when you create a specific instance), memory is reserved for the color and you can give this specific instance a color. Since these attributes are specific, they are non-static.



                    Static fields and methods are shared with all instances. They are for values which are specific to the class and not a specific instance. For methods, this usually are global helper methods (like Integer.parseInt()). For fields, it's usually constants (like car types, i.e. something where you have a limited set which doesn't change often).



                    To solve your problem, you need to instantiate an instance (create an object) of your class so the runtime can reserve memory for the instance (otherwise, different instances would overwrite each other which you don't want).



                    In your case, try this code as a starting block:



                    public static void main (String args)
                    {
                    try
                    {
                    MyProgram7 obj = new MyProgram7 ();
                    obj.run (args);
                    }
                    catch (Exception e)
                    {
                    e.printStackTrace ();
                    }
                    }

                    // instance variables here

                    public void run (String args) throws Exception
                    {
                    // put your code here
                    }


                    The new main() method creates an instance of the class it contains (sounds strange but since main() is created with the class instead of with the instance, it can do this) and then calls an instance method (run()).






                    share|improve this answer
























                    • I am explaining this at the moment to our new colleague - thanks for this great explanation. This should the acceptet answer.

                      – Supahupe
                      Jan 9 '18 at 13:45
















                    240












                    240








                    240







                    You must understand the difference between a class and an instance of that class. If you see a car on the street, you know immediately that it's a car even if you can't see which model or type. This is because you compare what you see with the class "car". The class contains which is similar to all cars. Think of it as a template or an idea.



                    At the same time, the car you see is an instance of the class "car" since it has all the properties which you expect: There is someone driving it, it has an engine, wheels.



                    So the class says "all cars have a color" and the instance says "this specific car is red".



                    In the OO world, you define the class and inside the class, you define a field of type Color. When the class is instantiated (when you create a specific instance), memory is reserved for the color and you can give this specific instance a color. Since these attributes are specific, they are non-static.



                    Static fields and methods are shared with all instances. They are for values which are specific to the class and not a specific instance. For methods, this usually are global helper methods (like Integer.parseInt()). For fields, it's usually constants (like car types, i.e. something where you have a limited set which doesn't change often).



                    To solve your problem, you need to instantiate an instance (create an object) of your class so the runtime can reserve memory for the instance (otherwise, different instances would overwrite each other which you don't want).



                    In your case, try this code as a starting block:



                    public static void main (String args)
                    {
                    try
                    {
                    MyProgram7 obj = new MyProgram7 ();
                    obj.run (args);
                    }
                    catch (Exception e)
                    {
                    e.printStackTrace ();
                    }
                    }

                    // instance variables here

                    public void run (String args) throws Exception
                    {
                    // put your code here
                    }


                    The new main() method creates an instance of the class it contains (sounds strange but since main() is created with the class instead of with the instance, it can do this) and then calls an instance method (run()).






                    share|improve this answer













                    You must understand the difference between a class and an instance of that class. If you see a car on the street, you know immediately that it's a car even if you can't see which model or type. This is because you compare what you see with the class "car". The class contains which is similar to all cars. Think of it as a template or an idea.



                    At the same time, the car you see is an instance of the class "car" since it has all the properties which you expect: There is someone driving it, it has an engine, wheels.



                    So the class says "all cars have a color" and the instance says "this specific car is red".



                    In the OO world, you define the class and inside the class, you define a field of type Color. When the class is instantiated (when you create a specific instance), memory is reserved for the color and you can give this specific instance a color. Since these attributes are specific, they are non-static.



                    Static fields and methods are shared with all instances. They are for values which are specific to the class and not a specific instance. For methods, this usually are global helper methods (like Integer.parseInt()). For fields, it's usually constants (like car types, i.e. something where you have a limited set which doesn't change often).



                    To solve your problem, you need to instantiate an instance (create an object) of your class so the runtime can reserve memory for the instance (otherwise, different instances would overwrite each other which you don't want).



                    In your case, try this code as a starting block:



                    public static void main (String args)
                    {
                    try
                    {
                    MyProgram7 obj = new MyProgram7 ();
                    obj.run (args);
                    }
                    catch (Exception e)
                    {
                    e.printStackTrace ();
                    }
                    }

                    // instance variables here

                    public void run (String args) throws Exception
                    {
                    // put your code here
                    }


                    The new main() method creates an instance of the class it contains (sounds strange but since main() is created with the class instead of with the instance, it can do this) and then calls an instance method (run()).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 1 '10 at 10:10









                    Aaron DigullaAaron Digulla

                    246k83468690




                    246k83468690













                    • I am explaining this at the moment to our new colleague - thanks for this great explanation. This should the acceptet answer.

                      – Supahupe
                      Jan 9 '18 at 13:45





















                    • I am explaining this at the moment to our new colleague - thanks for this great explanation. This should the acceptet answer.

                      – Supahupe
                      Jan 9 '18 at 13:45



















                    I am explaining this at the moment to our new colleague - thanks for this great explanation. This should the acceptet answer.

                    – Supahupe
                    Jan 9 '18 at 13:45







                    I am explaining this at the moment to our new colleague - thanks for this great explanation. This should the acceptet answer.

                    – Supahupe
                    Jan 9 '18 at 13:45















                    67














                    Static fields and methods are connected to the class itself and not its instances. If you have a class A, a 'normal' method b, and a static method c, and you make an instance a of your class A, the calls to A.c() and a.b() are valid. Method c() has no idea which instance is connected, so it cannot use non-static fields.



                    The solution for you is that you either make your fields static or your methods non-static. You main could look like this then:



                    class Programm {
                    public static void main(String args){
                    Programm programm = new Programm();
                    programm.start();
                    }
                    public void start(){
                    // can now access non-static fields
                    }
                    }





                    share|improve this answer






























                      67














                      Static fields and methods are connected to the class itself and not its instances. If you have a class A, a 'normal' method b, and a static method c, and you make an instance a of your class A, the calls to A.c() and a.b() are valid. Method c() has no idea which instance is connected, so it cannot use non-static fields.



                      The solution for you is that you either make your fields static or your methods non-static. You main could look like this then:



                      class Programm {
                      public static void main(String args){
                      Programm programm = new Programm();
                      programm.start();
                      }
                      public void start(){
                      // can now access non-static fields
                      }
                      }





                      share|improve this answer




























                        67












                        67








                        67







                        Static fields and methods are connected to the class itself and not its instances. If you have a class A, a 'normal' method b, and a static method c, and you make an instance a of your class A, the calls to A.c() and a.b() are valid. Method c() has no idea which instance is connected, so it cannot use non-static fields.



                        The solution for you is that you either make your fields static or your methods non-static. You main could look like this then:



                        class Programm {
                        public static void main(String args){
                        Programm programm = new Programm();
                        programm.start();
                        }
                        public void start(){
                        // can now access non-static fields
                        }
                        }





                        share|improve this answer















                        Static fields and methods are connected to the class itself and not its instances. If you have a class A, a 'normal' method b, and a static method c, and you make an instance a of your class A, the calls to A.c() and a.b() are valid. Method c() has no idea which instance is connected, so it cannot use non-static fields.



                        The solution for you is that you either make your fields static or your methods non-static. You main could look like this then:



                        class Programm {
                        public static void main(String args){
                        Programm programm = new Programm();
                        programm.start();
                        }
                        public void start(){
                        // can now access non-static fields
                        }
                        }






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 26 '17 at 3:37









                        Mark

                        12.1k33551




                        12.1k33551










                        answered Apr 1 '10 at 10:14









                        MnementhMnementh

                        30.8k38128195




                        30.8k38128195























                            30














                            The static keyword modifies the lifecycle of a method or variable within a class. A static method or variable is created at the time a class is loaded. A method or variable that is not declared as static is created only when the class is instantiated as an object for example by using the new operator.



                            The lifecycle of a class, in broad terms, is:




                            1. the source code for the class is written creating a template or
                              pattern or stamp which can then be used to

                            2. create an object with the new operator using the class to make an instance of the class as an actual object and then when done with the object

                            3. destroy the object reclaiming the resources it is holding such as memory during garbage collection.


                            In order to have an initial entry point for an application, Java has adopted the convention that the Java program must have a class that contains a method with an agreed upon or special name. This special method is called main(). Since the method must exist whether the class containing the main method has been instantiated or not, the main() method must be declared with the static modifier so that as soon as the class is loaded, the main() method is available.



                            The result is that when you start your Java application by a command line such as java helloworld a series of actions happen. First of all a Java Virtual Machine is started up and initialized. Next the helloworld.class file containing the compiled Java code is loaded into the Java Virtual Machine. Then the Java Virtual Machine looks for a method in the helloworld class that is called main(String args). this method must be static so that it will exist even though the class has not actually been instantiated as an object. The Java Virtual Machine does not create an instance of the class by creating an object from the class. It just loads the class and starts execution at the main() method.



                            So you need to create an instance of your class as an object and then you can access the methods and variables of the class that have not been declared with the static modifier. Once your Java program has started with the main() function you can then use any variables or methods that have the modifier of static since they exist as part of the class being loaded.



                            However, those variables and methods of the class which are outside of the main() method which do not have the static modifier can not be used until an instance of the class has been created as an object within the main() method. After creating the object you can then use the variables and methods of the object. An attempt to use the variables and methods of the class which do not have the static modifier without going through an object of the class is caught by the Java compiler at compile time and flagged as an error.



                            import java.io.*;

                            class helloworld {
                            int myInt; // this is a class variable that is unique to each object
                            static int myInt2; // this is a class variable shared by all objects of this class

                            static void main (String args) {
                            // this is the main entry point for this Java application
                            System.out.println ("Hello, Worldn");
                            myInt2 = 14; // able to access the static int
                            helloworld myWorld = new helloworld();
                            myWorld.myInt = 32; // able to access non-static through an object
                            }
                            }





                            share|improve this answer






























                              30














                              The static keyword modifies the lifecycle of a method or variable within a class. A static method or variable is created at the time a class is loaded. A method or variable that is not declared as static is created only when the class is instantiated as an object for example by using the new operator.



                              The lifecycle of a class, in broad terms, is:




                              1. the source code for the class is written creating a template or
                                pattern or stamp which can then be used to

                              2. create an object with the new operator using the class to make an instance of the class as an actual object and then when done with the object

                              3. destroy the object reclaiming the resources it is holding such as memory during garbage collection.


                              In order to have an initial entry point for an application, Java has adopted the convention that the Java program must have a class that contains a method with an agreed upon or special name. This special method is called main(). Since the method must exist whether the class containing the main method has been instantiated or not, the main() method must be declared with the static modifier so that as soon as the class is loaded, the main() method is available.



                              The result is that when you start your Java application by a command line such as java helloworld a series of actions happen. First of all a Java Virtual Machine is started up and initialized. Next the helloworld.class file containing the compiled Java code is loaded into the Java Virtual Machine. Then the Java Virtual Machine looks for a method in the helloworld class that is called main(String args). this method must be static so that it will exist even though the class has not actually been instantiated as an object. The Java Virtual Machine does not create an instance of the class by creating an object from the class. It just loads the class and starts execution at the main() method.



                              So you need to create an instance of your class as an object and then you can access the methods and variables of the class that have not been declared with the static modifier. Once your Java program has started with the main() function you can then use any variables or methods that have the modifier of static since they exist as part of the class being loaded.



                              However, those variables and methods of the class which are outside of the main() method which do not have the static modifier can not be used until an instance of the class has been created as an object within the main() method. After creating the object you can then use the variables and methods of the object. An attempt to use the variables and methods of the class which do not have the static modifier without going through an object of the class is caught by the Java compiler at compile time and flagged as an error.



                              import java.io.*;

                              class helloworld {
                              int myInt; // this is a class variable that is unique to each object
                              static int myInt2; // this is a class variable shared by all objects of this class

                              static void main (String args) {
                              // this is the main entry point for this Java application
                              System.out.println ("Hello, Worldn");
                              myInt2 = 14; // able to access the static int
                              helloworld myWorld = new helloworld();
                              myWorld.myInt = 32; // able to access non-static through an object
                              }
                              }





                              share|improve this answer




























                                30












                                30








                                30







                                The static keyword modifies the lifecycle of a method or variable within a class. A static method or variable is created at the time a class is loaded. A method or variable that is not declared as static is created only when the class is instantiated as an object for example by using the new operator.



                                The lifecycle of a class, in broad terms, is:




                                1. the source code for the class is written creating a template or
                                  pattern or stamp which can then be used to

                                2. create an object with the new operator using the class to make an instance of the class as an actual object and then when done with the object

                                3. destroy the object reclaiming the resources it is holding such as memory during garbage collection.


                                In order to have an initial entry point for an application, Java has adopted the convention that the Java program must have a class that contains a method with an agreed upon or special name. This special method is called main(). Since the method must exist whether the class containing the main method has been instantiated or not, the main() method must be declared with the static modifier so that as soon as the class is loaded, the main() method is available.



                                The result is that when you start your Java application by a command line such as java helloworld a series of actions happen. First of all a Java Virtual Machine is started up and initialized. Next the helloworld.class file containing the compiled Java code is loaded into the Java Virtual Machine. Then the Java Virtual Machine looks for a method in the helloworld class that is called main(String args). this method must be static so that it will exist even though the class has not actually been instantiated as an object. The Java Virtual Machine does not create an instance of the class by creating an object from the class. It just loads the class and starts execution at the main() method.



                                So you need to create an instance of your class as an object and then you can access the methods and variables of the class that have not been declared with the static modifier. Once your Java program has started with the main() function you can then use any variables or methods that have the modifier of static since they exist as part of the class being loaded.



                                However, those variables and methods of the class which are outside of the main() method which do not have the static modifier can not be used until an instance of the class has been created as an object within the main() method. After creating the object you can then use the variables and methods of the object. An attempt to use the variables and methods of the class which do not have the static modifier without going through an object of the class is caught by the Java compiler at compile time and flagged as an error.



                                import java.io.*;

                                class helloworld {
                                int myInt; // this is a class variable that is unique to each object
                                static int myInt2; // this is a class variable shared by all objects of this class

                                static void main (String args) {
                                // this is the main entry point for this Java application
                                System.out.println ("Hello, Worldn");
                                myInt2 = 14; // able to access the static int
                                helloworld myWorld = new helloworld();
                                myWorld.myInt = 32; // able to access non-static through an object
                                }
                                }





                                share|improve this answer















                                The static keyword modifies the lifecycle of a method or variable within a class. A static method or variable is created at the time a class is loaded. A method or variable that is not declared as static is created only when the class is instantiated as an object for example by using the new operator.



                                The lifecycle of a class, in broad terms, is:




                                1. the source code for the class is written creating a template or
                                  pattern or stamp which can then be used to

                                2. create an object with the new operator using the class to make an instance of the class as an actual object and then when done with the object

                                3. destroy the object reclaiming the resources it is holding such as memory during garbage collection.


                                In order to have an initial entry point for an application, Java has adopted the convention that the Java program must have a class that contains a method with an agreed upon or special name. This special method is called main(). Since the method must exist whether the class containing the main method has been instantiated or not, the main() method must be declared with the static modifier so that as soon as the class is loaded, the main() method is available.



                                The result is that when you start your Java application by a command line such as java helloworld a series of actions happen. First of all a Java Virtual Machine is started up and initialized. Next the helloworld.class file containing the compiled Java code is loaded into the Java Virtual Machine. Then the Java Virtual Machine looks for a method in the helloworld class that is called main(String args). this method must be static so that it will exist even though the class has not actually been instantiated as an object. The Java Virtual Machine does not create an instance of the class by creating an object from the class. It just loads the class and starts execution at the main() method.



                                So you need to create an instance of your class as an object and then you can access the methods and variables of the class that have not been declared with the static modifier. Once your Java program has started with the main() function you can then use any variables or methods that have the modifier of static since they exist as part of the class being loaded.



                                However, those variables and methods of the class which are outside of the main() method which do not have the static modifier can not be used until an instance of the class has been created as an object within the main() method. After creating the object you can then use the variables and methods of the object. An attempt to use the variables and methods of the class which do not have the static modifier without going through an object of the class is caught by the Java compiler at compile time and flagged as an error.



                                import java.io.*;

                                class helloworld {
                                int myInt; // this is a class variable that is unique to each object
                                static int myInt2; // this is a class variable shared by all objects of this class

                                static void main (String args) {
                                // this is the main entry point for this Java application
                                System.out.println ("Hello, Worldn");
                                myInt2 = 14; // able to access the static int
                                helloworld myWorld = new helloworld();
                                myWorld.myInt = 32; // able to access non-static through an object
                                }
                                }






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Mar 8 '17 at 12:57

























                                answered Jan 16 '15 at 15:37









                                Richard ChambersRichard Chambers

                                9,76724066




                                9,76724066























                                    11














                                    Let's analyze your program first..
                                    In your program, your first method is main(), and keep it in mind it is the static method... Then you declare the local variable for that method (compareCount, low, high, etc..). The scope of this variable is only the declared method, regardless of it being a static or non static method. So you can't use those variables outside that method. This is the basic error u made.



                                    Then we come to next point. You told static is killing you. (It may be killing you but it only gives life to your program!!) First you must understand the basic thing.
                                    *Static method calls only the static method and use only the static variable.
                                    *Static variable or static method are not dependent on any instance of that class. (i.e. If you change any state of the static variable it will reflect in all objects of the class)
                                    *Because of this you call it as a class variable or a class method.
                                    And a lot more is there about the "static" keyword.
                                    I hope now you get the idea. First change the scope of the variable and declare it as a static (to be able to use it in static methods).



                                    And the advice for you is: you misunderstood the idea of the scope of the variables and static functionalities. Get clear idea about that.






                                    share|improve this answer






























                                      11














                                      Let's analyze your program first..
                                      In your program, your first method is main(), and keep it in mind it is the static method... Then you declare the local variable for that method (compareCount, low, high, etc..). The scope of this variable is only the declared method, regardless of it being a static or non static method. So you can't use those variables outside that method. This is the basic error u made.



                                      Then we come to next point. You told static is killing you. (It may be killing you but it only gives life to your program!!) First you must understand the basic thing.
                                      *Static method calls only the static method and use only the static variable.
                                      *Static variable or static method are not dependent on any instance of that class. (i.e. If you change any state of the static variable it will reflect in all objects of the class)
                                      *Because of this you call it as a class variable or a class method.
                                      And a lot more is there about the "static" keyword.
                                      I hope now you get the idea. First change the scope of the variable and declare it as a static (to be able to use it in static methods).



                                      And the advice for you is: you misunderstood the idea of the scope of the variables and static functionalities. Get clear idea about that.






                                      share|improve this answer




























                                        11












                                        11








                                        11







                                        Let's analyze your program first..
                                        In your program, your first method is main(), and keep it in mind it is the static method... Then you declare the local variable for that method (compareCount, low, high, etc..). The scope of this variable is only the declared method, regardless of it being a static or non static method. So you can't use those variables outside that method. This is the basic error u made.



                                        Then we come to next point. You told static is killing you. (It may be killing you but it only gives life to your program!!) First you must understand the basic thing.
                                        *Static method calls only the static method and use only the static variable.
                                        *Static variable or static method are not dependent on any instance of that class. (i.e. If you change any state of the static variable it will reflect in all objects of the class)
                                        *Because of this you call it as a class variable or a class method.
                                        And a lot more is there about the "static" keyword.
                                        I hope now you get the idea. First change the scope of the variable and declare it as a static (to be able to use it in static methods).



                                        And the advice for you is: you misunderstood the idea of the scope of the variables and static functionalities. Get clear idea about that.






                                        share|improve this answer















                                        Let's analyze your program first..
                                        In your program, your first method is main(), and keep it in mind it is the static method... Then you declare the local variable for that method (compareCount, low, high, etc..). The scope of this variable is only the declared method, regardless of it being a static or non static method. So you can't use those variables outside that method. This is the basic error u made.



                                        Then we come to next point. You told static is killing you. (It may be killing you but it only gives life to your program!!) First you must understand the basic thing.
                                        *Static method calls only the static method and use only the static variable.
                                        *Static variable or static method are not dependent on any instance of that class. (i.e. If you change any state of the static variable it will reflect in all objects of the class)
                                        *Because of this you call it as a class variable or a class method.
                                        And a lot more is there about the "static" keyword.
                                        I hope now you get the idea. First change the scope of the variable and declare it as a static (to be able to use it in static methods).



                                        And the advice for you is: you misunderstood the idea of the scope of the variables and static functionalities. Get clear idea about that.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Nov 16 '12 at 21:28









                                        Michal Trojanowski

                                        2,69911120




                                        2,69911120










                                        answered Jul 18 '11 at 9:10









                                        Suseendran.PSuseendran.P

                                        11912




                                        11912























                                            9














                                            The very basic thing is static variables or static methods are at class level. Class level variables or methods gets loaded prior to instance level methods or variables.And obviously the thing which is not loaded can not be used. So java compiler not letting the things to be handled at run time resolves at compile time. That's why it is giving you error non-static things can not be referred from static context. You just need to read about Class Level Scope, Instance Level Scope and Local Scope.






                                            share|improve this answer




























                                              9














                                              The very basic thing is static variables or static methods are at class level. Class level variables or methods gets loaded prior to instance level methods or variables.And obviously the thing which is not loaded can not be used. So java compiler not letting the things to be handled at run time resolves at compile time. That's why it is giving you error non-static things can not be referred from static context. You just need to read about Class Level Scope, Instance Level Scope and Local Scope.






                                              share|improve this answer


























                                                9












                                                9








                                                9







                                                The very basic thing is static variables or static methods are at class level. Class level variables or methods gets loaded prior to instance level methods or variables.And obviously the thing which is not loaded can not be used. So java compiler not letting the things to be handled at run time resolves at compile time. That's why it is giving you error non-static things can not be referred from static context. You just need to read about Class Level Scope, Instance Level Scope and Local Scope.






                                                share|improve this answer













                                                The very basic thing is static variables or static methods are at class level. Class level variables or methods gets loaded prior to instance level methods or variables.And obviously the thing which is not loaded can not be used. So java compiler not letting the things to be handled at run time resolves at compile time. That's why it is giving you error non-static things can not be referred from static context. You just need to read about Class Level Scope, Instance Level Scope and Local Scope.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jul 24 '13 at 13:26









                                                Ajay BhojakAjay Bhojak

                                                1,097915




                                                1,097915























                                                    8














                                                    To be able to access them from your static methods they need to be static member variables, like this:



                                                    public class MyProgram7 {
                                                    static Scanner scan = new Scanner(System.in);
                                                    static int compareCount = 0;
                                                    static int low = 0;
                                                    static int high = 0;
                                                    static int mid = 0;
                                                    static int key = 0;
                                                    static Scanner temp;
                                                    static intlist;
                                                    static String menu, outputString;
                                                    static int option = 1;
                                                    static boolean found = false;

                                                    public static void main (Stringargs) throws IOException {
                                                    ...





                                                    share|improve this answer




























                                                      8














                                                      To be able to access them from your static methods they need to be static member variables, like this:



                                                      public class MyProgram7 {
                                                      static Scanner scan = new Scanner(System.in);
                                                      static int compareCount = 0;
                                                      static int low = 0;
                                                      static int high = 0;
                                                      static int mid = 0;
                                                      static int key = 0;
                                                      static Scanner temp;
                                                      static intlist;
                                                      static String menu, outputString;
                                                      static int option = 1;
                                                      static boolean found = false;

                                                      public static void main (Stringargs) throws IOException {
                                                      ...





                                                      share|improve this answer


























                                                        8












                                                        8








                                                        8







                                                        To be able to access them from your static methods they need to be static member variables, like this:



                                                        public class MyProgram7 {
                                                        static Scanner scan = new Scanner(System.in);
                                                        static int compareCount = 0;
                                                        static int low = 0;
                                                        static int high = 0;
                                                        static int mid = 0;
                                                        static int key = 0;
                                                        static Scanner temp;
                                                        static intlist;
                                                        static String menu, outputString;
                                                        static int option = 1;
                                                        static boolean found = false;

                                                        public static void main (Stringargs) throws IOException {
                                                        ...





                                                        share|improve this answer













                                                        To be able to access them from your static methods they need to be static member variables, like this:



                                                        public class MyProgram7 {
                                                        static Scanner scan = new Scanner(System.in);
                                                        static int compareCount = 0;
                                                        static int low = 0;
                                                        static int high = 0;
                                                        static int mid = 0;
                                                        static int key = 0;
                                                        static Scanner temp;
                                                        static intlist;
                                                        static String menu, outputString;
                                                        static int option = 1;
                                                        static boolean found = false;

                                                        public static void main (Stringargs) throws IOException {
                                                        ...






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Apr 1 '10 at 10:08









                                                        Nick MooreNick Moore

                                                        12.2k44977




                                                        12.2k44977























                                                            7














                                                            Now you can add/use instances with in the method



                                                            public class Myprogram7 {

                                                            Scanner scan;
                                                            int compareCount = 0;
                                                            int low = 0;
                                                            int high = 0;
                                                            int mid = 0;
                                                            int key = 0;
                                                            Scanner temp;
                                                            intlist;
                                                            String menu, outputString;
                                                            int option = 1;
                                                            boolean found = false;

                                                            private void readLine() {

                                                            }

                                                            private void findkey() {

                                                            }

                                                            private void printCount() {

                                                            }
                                                            public static void main(String args){

                                                            Myprogram7 myprg=new Myprogram7();
                                                            myprg.readLine();
                                                            myprg.findkey();
                                                            myprg.printCount();
                                                            }
                                                            }





                                                            share|improve this answer


























                                                            • Very solid example that I used as a template to revised a complex src file into a proper structure.

                                                              – XO.
                                                              Nov 27 '18 at 4:49
















                                                            7














                                                            Now you can add/use instances with in the method



                                                            public class Myprogram7 {

                                                            Scanner scan;
                                                            int compareCount = 0;
                                                            int low = 0;
                                                            int high = 0;
                                                            int mid = 0;
                                                            int key = 0;
                                                            Scanner temp;
                                                            intlist;
                                                            String menu, outputString;
                                                            int option = 1;
                                                            boolean found = false;

                                                            private void readLine() {

                                                            }

                                                            private void findkey() {

                                                            }

                                                            private void printCount() {

                                                            }
                                                            public static void main(String args){

                                                            Myprogram7 myprg=new Myprogram7();
                                                            myprg.readLine();
                                                            myprg.findkey();
                                                            myprg.printCount();
                                                            }
                                                            }





                                                            share|improve this answer


























                                                            • Very solid example that I used as a template to revised a complex src file into a proper structure.

                                                              – XO.
                                                              Nov 27 '18 at 4:49














                                                            7












                                                            7








                                                            7







                                                            Now you can add/use instances with in the method



                                                            public class Myprogram7 {

                                                            Scanner scan;
                                                            int compareCount = 0;
                                                            int low = 0;
                                                            int high = 0;
                                                            int mid = 0;
                                                            int key = 0;
                                                            Scanner temp;
                                                            intlist;
                                                            String menu, outputString;
                                                            int option = 1;
                                                            boolean found = false;

                                                            private void readLine() {

                                                            }

                                                            private void findkey() {

                                                            }

                                                            private void printCount() {

                                                            }
                                                            public static void main(String args){

                                                            Myprogram7 myprg=new Myprogram7();
                                                            myprg.readLine();
                                                            myprg.findkey();
                                                            myprg.printCount();
                                                            }
                                                            }





                                                            share|improve this answer















                                                            Now you can add/use instances with in the method



                                                            public class Myprogram7 {

                                                            Scanner scan;
                                                            int compareCount = 0;
                                                            int low = 0;
                                                            int high = 0;
                                                            int mid = 0;
                                                            int key = 0;
                                                            Scanner temp;
                                                            intlist;
                                                            String menu, outputString;
                                                            int option = 1;
                                                            boolean found = false;

                                                            private void readLine() {

                                                            }

                                                            private void findkey() {

                                                            }

                                                            private void printCount() {

                                                            }
                                                            public static void main(String args){

                                                            Myprogram7 myprg=new Myprogram7();
                                                            myprg.readLine();
                                                            myprg.findkey();
                                                            myprg.printCount();
                                                            }
                                                            }






                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Sep 24 '13 at 10:24









                                                            Zaheer Ahmed

                                                            22.3k106099




                                                            22.3k106099










                                                            answered Sep 24 '13 at 10:03









                                                            Sainath Patwary karnateSainath Patwary karnate

                                                            2,077918




                                                            2,077918













                                                            • Very solid example that I used as a template to revised a complex src file into a proper structure.

                                                              – XO.
                                                              Nov 27 '18 at 4:49



















                                                            • Very solid example that I used as a template to revised a complex src file into a proper structure.

                                                              – XO.
                                                              Nov 27 '18 at 4:49

















                                                            Very solid example that I used as a template to revised a complex src file into a proper structure.

                                                            – XO.
                                                            Nov 27 '18 at 4:49





                                                            Very solid example that I used as a template to revised a complex src file into a proper structure.

                                                            – XO.
                                                            Nov 27 '18 at 4:49











                                                            4














                                                            I will try to explain the static thing to you. First of all static variables do not belong to any particular instance of the class. They are recognized with the name of the class. Static methods again do not belong again to any particular instance. They can access only static variables. Imagine you call MyClass.myMethod() and myMethod is a static method. If you use non-static variables inside the method, how the hell on earth would it know which variables to use? That's why you can use from static methods only static variables. I repeat again they do NOT belong to any particular instance.






                                                            share|improve this answer




























                                                              4














                                                              I will try to explain the static thing to you. First of all static variables do not belong to any particular instance of the class. They are recognized with the name of the class. Static methods again do not belong again to any particular instance. They can access only static variables. Imagine you call MyClass.myMethod() and myMethod is a static method. If you use non-static variables inside the method, how the hell on earth would it know which variables to use? That's why you can use from static methods only static variables. I repeat again they do NOT belong to any particular instance.






                                                              share|improve this answer


























                                                                4












                                                                4








                                                                4







                                                                I will try to explain the static thing to you. First of all static variables do not belong to any particular instance of the class. They are recognized with the name of the class. Static methods again do not belong again to any particular instance. They can access only static variables. Imagine you call MyClass.myMethod() and myMethod is a static method. If you use non-static variables inside the method, how the hell on earth would it know which variables to use? That's why you can use from static methods only static variables. I repeat again they do NOT belong to any particular instance.






                                                                share|improve this answer













                                                                I will try to explain the static thing to you. First of all static variables do not belong to any particular instance of the class. They are recognized with the name of the class. Static methods again do not belong again to any particular instance. They can access only static variables. Imagine you call MyClass.myMethod() and myMethod is a static method. If you use non-static variables inside the method, how the hell on earth would it know which variables to use? That's why you can use from static methods only static variables. I repeat again they do NOT belong to any particular instance.







                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered Apr 1 '10 at 10:13









                                                                Petar MinchevPetar Minchev

                                                                39.9k1184112




                                                                39.9k1184112























                                                                    2















                                                                    • The first thing is to know the difference between an instance of a class, and the class itself. A class models certain properties, and the behaviour of the whole in the context of those properties. An instance will define specific values for those properties.


                                                                    • Anything bound to the static keyword is available in the context of the class rather than in the context of an instance of the class



                                                                    • As a corollary to the above




                                                                      1. variables within a method can not be static

                                                                      2. static fields, and methods must be invoked using the class-name e.g. MyProgram7.main(...)



                                                                    • The lifetime of a static field/method is equivalent to the lifetime of your application



                                                                    E.g.
                                                                    Say, car has the property colour, and exhibits the behaviour 'motion'.
                                                                    An instance of the car would be a Red Volkswagen Beetle in motion at 25kmph.



                                                                    Now a static property of the car would be the number of wheels (4) on the road, and this would apply to all cars.



                                                                    HTH






                                                                    share|improve this answer




























                                                                      2















                                                                      • The first thing is to know the difference between an instance of a class, and the class itself. A class models certain properties, and the behaviour of the whole in the context of those properties. An instance will define specific values for those properties.


                                                                      • Anything bound to the static keyword is available in the context of the class rather than in the context of an instance of the class



                                                                      • As a corollary to the above




                                                                        1. variables within a method can not be static

                                                                        2. static fields, and methods must be invoked using the class-name e.g. MyProgram7.main(...)



                                                                      • The lifetime of a static field/method is equivalent to the lifetime of your application



                                                                      E.g.
                                                                      Say, car has the property colour, and exhibits the behaviour 'motion'.
                                                                      An instance of the car would be a Red Volkswagen Beetle in motion at 25kmph.



                                                                      Now a static property of the car would be the number of wheels (4) on the road, and this would apply to all cars.



                                                                      HTH






                                                                      share|improve this answer


























                                                                        2












                                                                        2








                                                                        2








                                                                        • The first thing is to know the difference between an instance of a class, and the class itself. A class models certain properties, and the behaviour of the whole in the context of those properties. An instance will define specific values for those properties.


                                                                        • Anything bound to the static keyword is available in the context of the class rather than in the context of an instance of the class



                                                                        • As a corollary to the above




                                                                          1. variables within a method can not be static

                                                                          2. static fields, and methods must be invoked using the class-name e.g. MyProgram7.main(...)



                                                                        • The lifetime of a static field/method is equivalent to the lifetime of your application



                                                                        E.g.
                                                                        Say, car has the property colour, and exhibits the behaviour 'motion'.
                                                                        An instance of the car would be a Red Volkswagen Beetle in motion at 25kmph.



                                                                        Now a static property of the car would be the number of wheels (4) on the road, and this would apply to all cars.



                                                                        HTH






                                                                        share|improve this answer














                                                                        • The first thing is to know the difference between an instance of a class, and the class itself. A class models certain properties, and the behaviour of the whole in the context of those properties. An instance will define specific values for those properties.


                                                                        • Anything bound to the static keyword is available in the context of the class rather than in the context of an instance of the class



                                                                        • As a corollary to the above




                                                                          1. variables within a method can not be static

                                                                          2. static fields, and methods must be invoked using the class-name e.g. MyProgram7.main(...)



                                                                        • The lifetime of a static field/method is equivalent to the lifetime of your application



                                                                        E.g.
                                                                        Say, car has the property colour, and exhibits the behaviour 'motion'.
                                                                        An instance of the car would be a Red Volkswagen Beetle in motion at 25kmph.



                                                                        Now a static property of the car would be the number of wheels (4) on the road, and this would apply to all cars.



                                                                        HTH







                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Apr 1 '10 at 11:55









                                                                        EveryoneEveryone

                                                                        1,64412334




                                                                        1,64412334























                                                                            1














                                                                            This is bit diff to explain about static key word for all beginners.

                                                                            You wil get to know it clearly when you work more with Classes and Objects.



                                                                            |*| Static : Static items can be called with Class Name

                                                                            If you observe in codes, Some functions are directly called with Class names like



                                                                            NamCls.NamFnc();

                                                                            System.out.println();


                                                                            This is because NamFnc and println wil be declared using key word static before them.



                                                                            |*| Non Static :Non Static items can be called with Class Variable

                                                                            If its not static, you need a variable of the class,

                                                                            put dot after the class variable and

                                                                            then call function.



                                                                            NamCls NamObjVar = new NamCls();
                                                                            NamObjVar.NamFnc();





                                                                            Below code explains you neatly

                                                                            |*| Static and non Static function in class :



                                                                            public class NamCls
                                                                            {
                                                                            public static void main(String args)
                                                                            {
                                                                            PlsPrnFnc("Tst Txt");

                                                                            NamCls NamObjVar = new NamCls();
                                                                            NamObjVar.PrnFnc("Tst Txt");
                                                                            }

                                                                            static void PlsPrnFnc(String SrgPsgVal)
                                                                            {
                                                                            System.out.println(SrgPsgVal);
                                                                            }

                                                                            void PrnFnc(String SrgPsgVal)
                                                                            {
                                                                            System.out.println(SrgPsgVal);
                                                                            }
                                                                            }





                                                                            |*| Static and non Static Class inside a Class :

                                                                            public class NamCls
                                                                            {
                                                                            public static void main(String args)
                                                                            {
                                                                            NamTicCls NamTicVaj = new NamTicCls();
                                                                            NamTicVaj.PrnFnc("Tst Txt");

                                                                            NamCls NamObjVar = new NamCls();
                                                                            NamNicCls NamNicVar = NamObjVar.new NamNicCls();
                                                                            NamNicVar.PrnFnc("Tst Txt");
                                                                            }

                                                                            static class NamTicCls
                                                                            {
                                                                            void PrnFnc(String SrgPsgVal)
                                                                            {
                                                                            System.out.println(SrgPsgVal);
                                                                            }
                                                                            }

                                                                            class NamNicCls
                                                                            {
                                                                            void PrnFnc(String SrgPsgVal)
                                                                            {
                                                                            System.out.println(SrgPsgVal);
                                                                            }
                                                                            }
                                                                            }





                                                                            share|improve this answer




























                                                                              1














                                                                              This is bit diff to explain about static key word for all beginners.

                                                                              You wil get to know it clearly when you work more with Classes and Objects.



                                                                              |*| Static : Static items can be called with Class Name

                                                                              If you observe in codes, Some functions are directly called with Class names like



                                                                              NamCls.NamFnc();

                                                                              System.out.println();


                                                                              This is because NamFnc and println wil be declared using key word static before them.



                                                                              |*| Non Static :Non Static items can be called with Class Variable

                                                                              If its not static, you need a variable of the class,

                                                                              put dot after the class variable and

                                                                              then call function.



                                                                              NamCls NamObjVar = new NamCls();
                                                                              NamObjVar.NamFnc();





                                                                              Below code explains you neatly

                                                                              |*| Static and non Static function in class :



                                                                              public class NamCls
                                                                              {
                                                                              public static void main(String args)
                                                                              {
                                                                              PlsPrnFnc("Tst Txt");

                                                                              NamCls NamObjVar = new NamCls();
                                                                              NamObjVar.PrnFnc("Tst Txt");
                                                                              }

                                                                              static void PlsPrnFnc(String SrgPsgVal)
                                                                              {
                                                                              System.out.println(SrgPsgVal);
                                                                              }

                                                                              void PrnFnc(String SrgPsgVal)
                                                                              {
                                                                              System.out.println(SrgPsgVal);
                                                                              }
                                                                              }





                                                                              |*| Static and non Static Class inside a Class :

                                                                              public class NamCls
                                                                              {
                                                                              public static void main(String args)
                                                                              {
                                                                              NamTicCls NamTicVaj = new NamTicCls();
                                                                              NamTicVaj.PrnFnc("Tst Txt");

                                                                              NamCls NamObjVar = new NamCls();
                                                                              NamNicCls NamNicVar = NamObjVar.new NamNicCls();
                                                                              NamNicVar.PrnFnc("Tst Txt");
                                                                              }

                                                                              static class NamTicCls
                                                                              {
                                                                              void PrnFnc(String SrgPsgVal)
                                                                              {
                                                                              System.out.println(SrgPsgVal);
                                                                              }
                                                                              }

                                                                              class NamNicCls
                                                                              {
                                                                              void PrnFnc(String SrgPsgVal)
                                                                              {
                                                                              System.out.println(SrgPsgVal);
                                                                              }
                                                                              }
                                                                              }





                                                                              share|improve this answer


























                                                                                1












                                                                                1








                                                                                1







                                                                                This is bit diff to explain about static key word for all beginners.

                                                                                You wil get to know it clearly when you work more with Classes and Objects.



                                                                                |*| Static : Static items can be called with Class Name

                                                                                If you observe in codes, Some functions are directly called with Class names like



                                                                                NamCls.NamFnc();

                                                                                System.out.println();


                                                                                This is because NamFnc and println wil be declared using key word static before them.



                                                                                |*| Non Static :Non Static items can be called with Class Variable

                                                                                If its not static, you need a variable of the class,

                                                                                put dot after the class variable and

                                                                                then call function.



                                                                                NamCls NamObjVar = new NamCls();
                                                                                NamObjVar.NamFnc();





                                                                                Below code explains you neatly

                                                                                |*| Static and non Static function in class :



                                                                                public class NamCls
                                                                                {
                                                                                public static void main(String args)
                                                                                {
                                                                                PlsPrnFnc("Tst Txt");

                                                                                NamCls NamObjVar = new NamCls();
                                                                                NamObjVar.PrnFnc("Tst Txt");
                                                                                }

                                                                                static void PlsPrnFnc(String SrgPsgVal)
                                                                                {
                                                                                System.out.println(SrgPsgVal);
                                                                                }

                                                                                void PrnFnc(String SrgPsgVal)
                                                                                {
                                                                                System.out.println(SrgPsgVal);
                                                                                }
                                                                                }





                                                                                |*| Static and non Static Class inside a Class :

                                                                                public class NamCls
                                                                                {
                                                                                public static void main(String args)
                                                                                {
                                                                                NamTicCls NamTicVaj = new NamTicCls();
                                                                                NamTicVaj.PrnFnc("Tst Txt");

                                                                                NamCls NamObjVar = new NamCls();
                                                                                NamNicCls NamNicVar = NamObjVar.new NamNicCls();
                                                                                NamNicVar.PrnFnc("Tst Txt");
                                                                                }

                                                                                static class NamTicCls
                                                                                {
                                                                                void PrnFnc(String SrgPsgVal)
                                                                                {
                                                                                System.out.println(SrgPsgVal);
                                                                                }
                                                                                }

                                                                                class NamNicCls
                                                                                {
                                                                                void PrnFnc(String SrgPsgVal)
                                                                                {
                                                                                System.out.println(SrgPsgVal);
                                                                                }
                                                                                }
                                                                                }





                                                                                share|improve this answer













                                                                                This is bit diff to explain about static key word for all beginners.

                                                                                You wil get to know it clearly when you work more with Classes and Objects.



                                                                                |*| Static : Static items can be called with Class Name

                                                                                If you observe in codes, Some functions are directly called with Class names like



                                                                                NamCls.NamFnc();

                                                                                System.out.println();


                                                                                This is because NamFnc and println wil be declared using key word static before them.



                                                                                |*| Non Static :Non Static items can be called with Class Variable

                                                                                If its not static, you need a variable of the class,

                                                                                put dot after the class variable and

                                                                                then call function.



                                                                                NamCls NamObjVar = new NamCls();
                                                                                NamObjVar.NamFnc();





                                                                                Below code explains you neatly

                                                                                |*| Static and non Static function in class :



                                                                                public class NamCls
                                                                                {
                                                                                public static void main(String args)
                                                                                {
                                                                                PlsPrnFnc("Tst Txt");

                                                                                NamCls NamObjVar = new NamCls();
                                                                                NamObjVar.PrnFnc("Tst Txt");
                                                                                }

                                                                                static void PlsPrnFnc(String SrgPsgVal)
                                                                                {
                                                                                System.out.println(SrgPsgVal);
                                                                                }

                                                                                void PrnFnc(String SrgPsgVal)
                                                                                {
                                                                                System.out.println(SrgPsgVal);
                                                                                }
                                                                                }





                                                                                |*| Static and non Static Class inside a Class :

                                                                                public class NamCls
                                                                                {
                                                                                public static void main(String args)
                                                                                {
                                                                                NamTicCls NamTicVaj = new NamTicCls();
                                                                                NamTicVaj.PrnFnc("Tst Txt");

                                                                                NamCls NamObjVar = new NamCls();
                                                                                NamNicCls NamNicVar = NamObjVar.new NamNicCls();
                                                                                NamNicVar.PrnFnc("Tst Txt");
                                                                                }

                                                                                static class NamTicCls
                                                                                {
                                                                                void PrnFnc(String SrgPsgVal)
                                                                                {
                                                                                System.out.println(SrgPsgVal);
                                                                                }
                                                                                }

                                                                                class NamNicCls
                                                                                {
                                                                                void PrnFnc(String SrgPsgVal)
                                                                                {
                                                                                System.out.println(SrgPsgVal);
                                                                                }
                                                                                }
                                                                                }






                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered Jun 6 '17 at 22:31









                                                                                Sujay U NSujay U N

                                                                                1,9632244




                                                                                1,9632244























                                                                                    1














                                                                                    Before you call an instance method or instance variable It needs a object(Instance). When instance variable is called from static method compiler doesn't know which is the object this variable belongs to. Because static methods doesn't have an object (Only one copy always). When you call an instance variable or instance methods from instance method it refer the this object. It means the variable belongs to whatever object created and each object have it's own copy of instance methods and variables.



                                                                                    Static variables are marked as static and instance variables doesn't have specific keyword.






                                                                                    share|improve this answer




























                                                                                      1














                                                                                      Before you call an instance method or instance variable It needs a object(Instance). When instance variable is called from static method compiler doesn't know which is the object this variable belongs to. Because static methods doesn't have an object (Only one copy always). When you call an instance variable or instance methods from instance method it refer the this object. It means the variable belongs to whatever object created and each object have it's own copy of instance methods and variables.



                                                                                      Static variables are marked as static and instance variables doesn't have specific keyword.






                                                                                      share|improve this answer


























                                                                                        1












                                                                                        1








                                                                                        1







                                                                                        Before you call an instance method or instance variable It needs a object(Instance). When instance variable is called from static method compiler doesn't know which is the object this variable belongs to. Because static methods doesn't have an object (Only one copy always). When you call an instance variable or instance methods from instance method it refer the this object. It means the variable belongs to whatever object created and each object have it's own copy of instance methods and variables.



                                                                                        Static variables are marked as static and instance variables doesn't have specific keyword.






                                                                                        share|improve this answer













                                                                                        Before you call an instance method or instance variable It needs a object(Instance). When instance variable is called from static method compiler doesn't know which is the object this variable belongs to. Because static methods doesn't have an object (Only one copy always). When you call an instance variable or instance methods from instance method it refer the this object. It means the variable belongs to whatever object created and each object have it's own copy of instance methods and variables.



                                                                                        Static variables are marked as static and instance variables doesn't have specific keyword.







                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered May 25 '18 at 19:45









                                                                                        ultimatexultimatex

                                                                                        157112




                                                                                        157112























                                                                                            0














                                                                                            It is ClassLoader responsible to load the class files.Let's see what happens when we write our own classes.



                                                                                            Example 1:



                                                                                            class StaticTest {

                                                                                            static int a;
                                                                                            int b;
                                                                                            int c;
                                                                                            }


                                                                                            Now we can see that class "StaticTest" has 3 fields.But actually there is no existence of b,c member variable.But why ???. OK Lest's see. Here b,c are instance variable.Since instance variable gets the memory at the time of object creation. So here b,c are not getting any memory yet. That's why there is no existence of b,c. So There is only existence of a.
                                                                                            For ClassLoader it has only one information about a. ClassLoader yet not recognize b,c because it's object not instantiated yet.



                                                                                            Let's see another example:
                                                                                            Example 2:



                                                                                            class StaticTest {

                                                                                            public void display() {
                                                                                            System.out.println("Static Test");
                                                                                            }


                                                                                            public static void main(String cmd) {

                                                                                            display();
                                                                                            }

                                                                                            }


                                                                                            Now if we try to compile this code compiler will give CE error.
                                                                                            CE: non-static method display() cannot be referenced from a static context.



                                                                                            Now For ClassLoader it looks like:



                                                                                            class StaticTest {

                                                                                            public static void main(String cmd) {

                                                                                            display();
                                                                                            }

                                                                                            }


                                                                                            In Example 2 CE error is because we call non static method from a static context. So it is not possible for ClassLoader to recognize method display() at compile time.So compile time error is occurred.






                                                                                            share|improve this answer


























                                                                                            • Probably submitted your answer by accident before you managed to finish it? Please edit it and add the missing content, thanks!

                                                                                              – plamut
                                                                                              Sep 3 '15 at 12:27
















                                                                                            0














                                                                                            It is ClassLoader responsible to load the class files.Let's see what happens when we write our own classes.



                                                                                            Example 1:



                                                                                            class StaticTest {

                                                                                            static int a;
                                                                                            int b;
                                                                                            int c;
                                                                                            }


                                                                                            Now we can see that class "StaticTest" has 3 fields.But actually there is no existence of b,c member variable.But why ???. OK Lest's see. Here b,c are instance variable.Since instance variable gets the memory at the time of object creation. So here b,c are not getting any memory yet. That's why there is no existence of b,c. So There is only existence of a.
                                                                                            For ClassLoader it has only one information about a. ClassLoader yet not recognize b,c because it's object not instantiated yet.



                                                                                            Let's see another example:
                                                                                            Example 2:



                                                                                            class StaticTest {

                                                                                            public void display() {
                                                                                            System.out.println("Static Test");
                                                                                            }


                                                                                            public static void main(String cmd) {

                                                                                            display();
                                                                                            }

                                                                                            }


                                                                                            Now if we try to compile this code compiler will give CE error.
                                                                                            CE: non-static method display() cannot be referenced from a static context.



                                                                                            Now For ClassLoader it looks like:



                                                                                            class StaticTest {

                                                                                            public static void main(String cmd) {

                                                                                            display();
                                                                                            }

                                                                                            }


                                                                                            In Example 2 CE error is because we call non static method from a static context. So it is not possible for ClassLoader to recognize method display() at compile time.So compile time error is occurred.






                                                                                            share|improve this answer


























                                                                                            • Probably submitted your answer by accident before you managed to finish it? Please edit it and add the missing content, thanks!

                                                                                              – plamut
                                                                                              Sep 3 '15 at 12:27














                                                                                            0












                                                                                            0








                                                                                            0







                                                                                            It is ClassLoader responsible to load the class files.Let's see what happens when we write our own classes.



                                                                                            Example 1:



                                                                                            class StaticTest {

                                                                                            static int a;
                                                                                            int b;
                                                                                            int c;
                                                                                            }


                                                                                            Now we can see that class "StaticTest" has 3 fields.But actually there is no existence of b,c member variable.But why ???. OK Lest's see. Here b,c are instance variable.Since instance variable gets the memory at the time of object creation. So here b,c are not getting any memory yet. That's why there is no existence of b,c. So There is only existence of a.
                                                                                            For ClassLoader it has only one information about a. ClassLoader yet not recognize b,c because it's object not instantiated yet.



                                                                                            Let's see another example:
                                                                                            Example 2:



                                                                                            class StaticTest {

                                                                                            public void display() {
                                                                                            System.out.println("Static Test");
                                                                                            }


                                                                                            public static void main(String cmd) {

                                                                                            display();
                                                                                            }

                                                                                            }


                                                                                            Now if we try to compile this code compiler will give CE error.
                                                                                            CE: non-static method display() cannot be referenced from a static context.



                                                                                            Now For ClassLoader it looks like:



                                                                                            class StaticTest {

                                                                                            public static void main(String cmd) {

                                                                                            display();
                                                                                            }

                                                                                            }


                                                                                            In Example 2 CE error is because we call non static method from a static context. So it is not possible for ClassLoader to recognize method display() at compile time.So compile time error is occurred.






                                                                                            share|improve this answer















                                                                                            It is ClassLoader responsible to load the class files.Let's see what happens when we write our own classes.



                                                                                            Example 1:



                                                                                            class StaticTest {

                                                                                            static int a;
                                                                                            int b;
                                                                                            int c;
                                                                                            }


                                                                                            Now we can see that class "StaticTest" has 3 fields.But actually there is no existence of b,c member variable.But why ???. OK Lest's see. Here b,c are instance variable.Since instance variable gets the memory at the time of object creation. So here b,c are not getting any memory yet. That's why there is no existence of b,c. So There is only existence of a.
                                                                                            For ClassLoader it has only one information about a. ClassLoader yet not recognize b,c because it's object not instantiated yet.



                                                                                            Let's see another example:
                                                                                            Example 2:



                                                                                            class StaticTest {

                                                                                            public void display() {
                                                                                            System.out.println("Static Test");
                                                                                            }


                                                                                            public static void main(String cmd) {

                                                                                            display();
                                                                                            }

                                                                                            }


                                                                                            Now if we try to compile this code compiler will give CE error.
                                                                                            CE: non-static method display() cannot be referenced from a static context.



                                                                                            Now For ClassLoader it looks like:



                                                                                            class StaticTest {

                                                                                            public static void main(String cmd) {

                                                                                            display();
                                                                                            }

                                                                                            }


                                                                                            In Example 2 CE error is because we call non static method from a static context. So it is not possible for ClassLoader to recognize method display() at compile time.So compile time error is occurred.







                                                                                            share|improve this answer














                                                                                            share|improve this answer



                                                                                            share|improve this answer








                                                                                            edited Sep 3 '15 at 13:10

























                                                                                            answered Sep 3 '15 at 12:21









                                                                                            Newaz Sharif AmitNewaz Sharif Amit

                                                                                            9310




                                                                                            9310













                                                                                            • Probably submitted your answer by accident before you managed to finish it? Please edit it and add the missing content, thanks!

                                                                                              – plamut
                                                                                              Sep 3 '15 at 12:27



















                                                                                            • Probably submitted your answer by accident before you managed to finish it? Please edit it and add the missing content, thanks!

                                                                                              – plamut
                                                                                              Sep 3 '15 at 12:27

















                                                                                            Probably submitted your answer by accident before you managed to finish it? Please edit it and add the missing content, thanks!

                                                                                            – plamut
                                                                                            Sep 3 '15 at 12:27





                                                                                            Probably submitted your answer by accident before you managed to finish it? Please edit it and add the missing content, thanks!

                                                                                            – plamut
                                                                                            Sep 3 '15 at 12:27



                                                                                            Popular posts from this blog

                                                                                            Homophylophilia

                                                                                            Updating UILabel text programmatically using a function

                                                                                            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage