-
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?
-
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?
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks