I have a simple Java theory question. If I write a class that contains a main() method along with some other methods, and within that main method invoke an instance of the class (say new Class()), I'm a bit confused why a recursion doesn't occur. Let's say I'm writing a graphing program, where the other methods in the class create a window and plot data; in the main method I call an instance of the class itself, and yet only one window appears. That's great, and it's what I wanted, but intuition suggests that if I create an instance of a class from within itself, some sort of recursion should occur. What prevents this? Here is an example (in my mind, I'm wondering what prevents unwanted recursion):

The main method is called by the JVM at the beginning of your execution. Instantiating new objects doesn't call the main method.
–
jlordoFeb 7 '13 at 18:42

When you instantiate a class(s), the static parts of that class are shared between the "concrete" objects that have been instantiated. That is why there is no "recursion". Also, you should format your code with proper indentation when you post code related material on this site.
–
user1043000Feb 7 '13 at 18:47

4 Answers
4

I think you're confusing the main method - which is just the entry point of the program - with a constructor.

For example, if you wrote:

public class Example {
public Example() {
new Example(); // Recursive call
}
public static void main(String[] args) {
// Will call the constructor, which will call itself... but main
// won't get called again.
new Example();
}
}

Yeah, I think I understand that above, however, recursive uses of methods don't really confuse me. I suppose that since the name of your method and the name of your class are the same, I'm not really sure whether your constructing an instance of the class or an instance of the method above (unless what I've said makes no sense).
–
A.G.Feb 7 '13 at 18:53

The new keyword does three things. First, it allocates enough memory for the instance variables of a class. Even classes which seem to have no instance variables will inherit a variable from Object that points to the class definition. Second, it calls the specified constructor to initialize those variables. Third, it returns a reference to where the memory is.
–
Mel NicholsonFeb 7 '13 at 18:59

@A.G.: There's no such thing as "an instance of the method". I don't have a method called Example - I have a constructor. I suggest you look up constructors in whatever book you're using while learning Java.
–
Jon SkeetFeb 7 '13 at 19:10

Recursion isn't a bad thing. I suspect you are asking why there isn't an infinite recursion.

The big concept you are missing is that when you call new Example() you are allocating memory and then invoking just one method (the constructor). The main() is only invoked at the start of the whole program unless you explicitly call it.

Since factorial doesn't use any instance variable, it could have been declared static and called as Calculator.factoral(5); without even using new, but I din't do that since showing off new was the whole point of the example.

OK, I think I'm understanding. I'm really trying to build a distinction in my mind between the class as a program and the class as an object. As a program, it begins at main. As an object, I'm calling Java's constructor method, which creates Example() as an object, where Example.method() is a way to access individual methods. If I wanted to create a recursion (finite or not), after invoking the constructor, would example.main() be the way to go? Purely a hypothetical question: I'm trying to understand the distinction.
–
A.G.Feb 7 '13 at 18:49

recursion happens whenever any group of methods call each other in a cycle (with the cycle of one being a special case where a method calls itself). Calling main() from Example() would complete the cycle and start the recursions (and without logic to stop it, infinite recursion)
–
Mel NicholsonFeb 7 '13 at 18:53

Typically it is bad practice to use the main method as your recursive routine. Take that out and put it in a separate method.
–
75inchpianistFeb 7 '13 at 18:54

I don't think he actually wants a recursive routine. He's just trying to figure out the sequence of execution.
–
Mel NicholsonFeb 7 '13 at 18:56

1

TO make it even clearer, you could write a class with no main at all that can calculate factorials. main is just the method the JVM looks for in the class it is told to start with. It's hard to format a comment though so I'll edit my answer...
–
Mel NicholsonFeb 7 '13 at 19:04

just because you have a main method in the class doesn' mean that it will be called every time you create the class.

Java looks for main as an entry point for the program, and calls it upon startup. Any objects you instantiate from their do not call main, since as far as java is concerned, it has done its job of making the entry point to the program.

So when a class is initialized as an object, is it safe/correct to believe that the'main' method is in some sense ignored? That is, new example() gives me access to example.method1, example.method2, example.method3, but not example.main (not to say that is such a method, I suppose I've never tried it).
–
A.G.Feb 7 '13 at 18:45

jlordo is right. you will still have access to the main method should you want to call it. it is a static member though, so rather than calling it like myNewObject.main(args) you would simply do Example.main(args). not sure why you'd want to do this though.
–
75inchpianistFeb 7 '13 at 18:47

@A.G.: when you instantiate an object, the main method does not execute (nor does any other method), but you can call it if you want to.
–
jlordoFeb 7 '13 at 18:48