Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Java

  1. #1
    Glenn Guest

    Java


    Why can't the following code access class instance variables
    public static void main(String[] arg)

    this main method is inclosed in my class definition. KI have instance variables
    that the class uses to move data between methods, however under the main
    function when I try to access an instance variable it gives me an error saying
    that static method main() can not access instance variable. My question is
    why not?

  2. #2
    Randy Charles Morin Guest

    Re: Java

    Any method with the static attribute has this issue. The reason is that
    static methods don't have instance data.
    --
    Randy Charles Morin
    Author of Programming Windows Services
    http://www.kbcafe.com

    Feel free to contact me by private email or messenger
    MSN Messenger - morin_randy@hotmail.com
    Yahoo Messenger - randymorin@yahoo.com

    "Glenn" <gplummer@telusplanet.net> wrote in message
    news:3bfd0172$1@147.208.176.211...
    >
    > Why can't the following code access class instance variables
    > public static void main(String[] arg)
    >
    > this main method is inclosed in my class definition. KI have instance

    variables
    > that the class uses to move data between methods, however under the main
    > function when I try to access an instance variable it gives me an error

    saying
    > that static method main() can not access instance variable. My question is
    > why not?




  3. #3
    Robert Guest

    Re: Java


    Glenn,

    This is an over-simplified response but, basically, the inability of static
    methods to access instance variables has to do with the difference between
    a static variable and an instance variable.
    Instance variables belong to a single instantiation of the class and do not
    exist until that instance is created.
    Static variables belong to the class as a whole (thus the term class variables)
    and are created as soon as the class is in focus (These variables "belong"
    to every instance of the class. Changing its value within one instance affects
    the value seen by all instances).
    When you call a static method, it has no way of knowing if an instance variable
    has been created yet; therefore, it may be attempting to work on something
    that doesn't exist.
    A simple rule of thumb: static methods can only call static variables.

    "Glenn" <gplummer@telusplanet.net> wrote:
    >
    >Why can't the following code access class instance variables
    >public static void main(String[] arg)
    >
    >this main method is inclosed in my class definition. KI have instance variables
    >that the class uses to move data between methods, however under the main
    >function when I try to access an instance variable it gives me an error

    saying
    >that static method main() can not access instance variable. My question

    is
    >why not?



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center