I am a beginner in Java. When I am reading about access modifier static i got a doubt- we declare main method as static but this says that in a static method one can call only static methods / static variables. How can in main we call other variables and methods? Ramu
Hi Ramu, Welcome to JavaRanch! Short answer: what main generally does in a "real" program is create some objects, and then call their methods, letting the objects do the work. It's not that main can only call static methods -- it's that without an object to call methods on, main can only call static methods. As soon as you create an object in main(), you can call its non-static methods. Get it?
Just to extend Ernest's answer a bit, main() is static so that JVM can load the class without the knowledge of how an instance of the class should be constructed. It may be a little non-obvious, but if you want to call non-static method from main(), you would need to create an instance of that class itself: