This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi Joseph, Welcome to JavaRanch! If a Java function calls itself, there will indeed be runaway recursion (you'll get a StackOverflowException when Java runs out of space.) But that's not what's happening here. Static functions like main() don't execute in the context of an object -- i.e., Java doesn't create any MiniMiniMusicApp objects when the application is started. If main() wants there to be an object, it has to create one, which is what you see happening here. There's no recursion: the main() function is creating a new object, which invokes the constructor "function" for the class. I'm sure this will become clearer as you read more of the book. [ February 20, 2004: Message edited by: Ernest Friedman-Hill ]
Originally posted by joseph corner: Hi Ernest, OK, I guess "static" is the crucial word here... And main() is always static isn't it? Thanks. J
main() is always static, yes. "static" is actually not really the crucial word. The important thing is that one function (main) is calling another function (the constructor) which doesn't turn around and call the first function again. The part about "static" was just to make it clear that main() is creating an object because one doesn't exist at that point.
Here's a simple example that WILL cause infinite looping [actually it will end when you run out of memory].
The key difference between this and your example is that within the constructor we create another instance. Creating that second instance will invoke the constructor again, creating yet a third instance, and so on, ad infinitum. In your example the "main()" method is invoked when you start java, but being "static" it doesn't require an instance of "MiniMiniMusicApp". Within the "main()" method a new instance of "MiniMiniMusicApp" is created, but that will invoke the constructor, not the "main()" method. Once that is done, the "play()" method is invoked on that instance. So there is no recursion involved. Recursion is a powerful tool, greatly simplifying certain operations. However it can also be very dangerous. And I've discovered that if you recurse too deeply there can be a serious performance problem. General rule, avoid it whenever possible.
hi, I do not totally agree with the explains given. Infact the keyword "static" is crucial in the sense that it makes sure that the second object of the same kind is not created . so if the main is called back then the keyword "static" prevents it from creating another object
Yea, I have to agree with the last post. The static isn't necessarily an issue. Objects call their own methods all the time...setters and getters are very commonly called from other methods within the same object. The trick is, you want to make sure method a() doesn't call method b() if method b() calls method a() in turn. Static is sort of a separate issue. The idea of static has to do with data or methods (static keyword can be applied to a method or a class variable) that is attached to the *class*...as opposed to an *instance* of the class, an object. Quick lesson: Lassie is an *instance* of the classification of Dog. Dog is the class. Lassie, Benji, and Rover are instances of that class. The class is the rubber stamp. The inked emblem left by a stamp is the instance. Most of the time, you'll want data to be associated with instances, as in the case of a dog, one might have a black coat, another brown, another yellow. It changes dog to dog. Then, there's data you want to associate with the class. For example, if you wanted to know how many dogs were in existence, the numDogs variable would have to be kept at the class level. If you did this:
Then every time you create an instance of the Dog class, it would carry around a variable called numDogs. Not very useful--each Dog instance has numDogs=1. On the other hand, if you say:
Now every single Dog instance, upon creation, is incrementing one single variable called numDogs which is kept at the class level. That means the *same* variable gets incremented, and any time you want you can check numDogs to see how many Dog instances were created. (Of course, as they die, they will not decrement the variable, so it will tell you the number created over all time, not the number still kicking around. If you want to subtract dogs that are dead, then you'd probably include a die() method that puts the object into an invalid state so it cannot be used anymore, or you could use the less efficient and less exact method of using a finalize() method--this is less exact because you never know when the JVM will actually call the finalize() method.) One thing that I find always confuses Java newbies is the syntax to access static data and methods. In the second code example above, numDogs is static and that means there need not be any instance existing for you to access it. Before creating any instance at all, you could say:
If you happen to have an instance, say lassie, Java (unfortunately) allows you to call static data and methods using the same syntax as if they weren't static. The reason they allow you to do this is a very bad one--someone theorized during the creation of the language that a business might convert some heavily used object's methods and data to static when they weren't before (or vice versa). By allowing the instance-type call, a business that does this can continue to compile code that used to call static parts of the class which have been changed to non-static. I think that this barely ever happens because usually if a business makes stuff static, it's for a reason...there's rarely a situation where it would make sense to make this kind of change. Regardless, this remains confusingly legal:
Lalit: No! The 'static' doesn't prevent the main from creating another instance, since the instance itself is not static like in this example:
It creates 100 As and the main Method is called 100 times. But only one 'count' is created, because it is a static MEMBER. Static Method do not depend on (non-static-)Members, and may be called without any Object. Since you don't have any Object of type 'A' when starting your program, you may only call a static Method (main). This Method may now call a First Object 'A', which rolls out an Application, or just the String '100'. joseph: Please chose an expressive Topic for your Threads, like 'static and main' - not that urgent 'need help', 'simple one', 'question' or 'hey'. [ February 21, 2004: Message edited by: Stefan Wagner ]