Calling Main Method From Main Method in Java…

Have you ever tried to call Main method from a Main method in java ?

If Yes, What was the result ?

Did you get an exception like .. “Exception in thread “main” java.lang.StackOverflowError” ?

We have few questions in mind….

  1. Is it possible to call Main method from a Main method in Java ?
  2. Do we get an exception or not ?

Now, after reading this post, you can say that YES… we can call Main method from a Main method without any error.

And for that just need to be strong in our basic concepts. We need logic.

Let’s start…

Following is the simple code…

public class MyClass {
public static void main(String[] args) {
System.out.println(“Hello from Main method”);
}
}

Output : Hello from Main method

 

Now, i am adding few lines in this code with explanation.

public class MyClass {
static String[] str = {
“one”,
“two”
}; // read explanation point 1

public static void main(String[] args) {
System.out.println(“Hello from Main method”);
main(str); // read explanation point 2
}
}

Explanation point 1 :

This is String Array variable. We initialize this variable at the time of declaration ( {“one”, “two”} ).

We make it static because we will use this “str” variable in Main method. As you already know, Main is static method and we can use only static variables in static method.

Explanation point 2

Here, we are calling main method from a Main method. If we write “main()” , it will give compilation error because Main method is taking string array as an argument.

So if we want to call main method, we need to put string array as an argument. So we are putting “str” in it .i.e String Array. i.e.

main(str);

But, after running this code, we will get an Exception because main method is calling itself again and again. And at a certain point it will throw an exception.

Output :  Exception in thread “main” java.lang.StackOverflowError

 

I hope everything is clear till now… take a breath…

Now, the story after interval is here…

We don’t want any exception…

We want to call Main method from a Main method without any problem…

To solve this problem, what we can do ? What we need ?

Think, Think… Think again…

 

Relax…. now, we are able to call Main method from a Main method.. with one problem only (it is calling itself again and again and again…. till we get an exception)

What if, we put and if else in Main method, so that it will call itself only for 5 or 10 times ?

Then we never get any exception… isn’t it ?

Let give it a try…

public class MyClass {
static String[] str = {
“one”,
“two”
};
static int i = 0;
public static void main(String[] args) {
if (i & lt; 10) {
System.out.println( & quot; Hello from Main method & quot;);
i++;
main(str);
}
}
}

Explanation :

Here, we are using static int variable named i with initial value “0”.

We put if condition and we call Main method only if i is less than 10. I think no need to explain i++ 😀

So output is :

Hello from Main method
Hello from Main method
Hello from Main method
Hello from Main method
Hello from Main method
Hello from Main method
Hello from Main method
Hello from Main method
Hello from Main method
Hello from Main method

Ohh WOW… we have done this… We called Main method from a Main method without any error…

I hope you guys understand this code… I have tried my best to explain you…

Any doubt, feel free to disturb comment box…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s