Sponsored Link •
|
Advertisement
|
Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.
Message:
> Why can't you access non-static variables from a static context (e.g. the main method). In other words, what would be the consequences of doing this if it was allowed? Non-static variables are part of the objects themselves. To use a non-static variable, you need to specify which instance of the class the variable belongs to. But with static methods, there might not even be any instances of the class. (For example, the static methods in Math run without instantiating a Math object.) And what if there are a multple instances of the class -- Which one do you operate on? In other words, non-static data cannot be used in static methods because there is no well-defined variable to operate on.
Replies: |
Sponsored Links
|