The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001



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.


why static methods can't use non-static data

Posted by Dan Bishop on March 22, 2001 at 5:50 PM

> 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.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us