wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes A simple one (?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A simple one (?)" Watch "A simple one (?)" New topic
Author

A simple one (?)

joseph corner
Ranch Hand

Joined: Feb 20, 2004
Posts: 50
Hi,
I've been reading Head First Java, and notice that in many of the examples given, a class will call a new instance of itself, e.g.
import javax.sound.midi.*;
public class MiniMiniMusicApp {

public static void main(String[] args) {
MiniMiniMusicApp mini = new MiniMiniMusicApp();
mini.play();
}
etc.

Why doesn't this cause a recursive loop? The new class should declare another new class, and so on, ad infinitum.
Thanks!
J
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
joseph corner
Ranch Hand

Joined: Feb 20, 2004
Posts: 50
Hi Ernest,
OK, I guess "static" is the crucial word here... And main() is always static isn't it?
Thanks.
J
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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.
joseph corner
Ranch Hand

Joined: Feb 20, 2004
Posts: 50
Got it!
Thanks All,
J
Lalit K Kumar
Ranch Hand

Joined: Jan 29, 2004
Posts: 32
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
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
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:

sev
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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 ]

http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple one (?)
 
Similar Threads
MiniMiniMusicApp? anyone else having issues with it?
Head First : MiniMiniMusicApp
Head First Java Chapter 11 MiniMiniMusicApp
Application doesn't quit -- MiniMiniMusicApp sample from Head First Java
Head First Java chapter 11 MiniMiniMusicApp