• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we recall the main method after the calling of JVM?  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
Can we recall the main method?
I'm trying a code to recall main method (after the calling of JVM). I know this doesn't make any sense but I'm trying this just like that. Please have a look on the code;


Thanks in advance
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikram Bhati wrote:Can we recall the main method?

Did you test it? Did it work?

If so, you have your answer.

Winston
 
Vikram Bhati
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Did you test it? Did it work?

If so, you have your answer.

Winston


No its not working.


I'm starter.
This question asked by interviewer that's why I posted it here and I was trying it with the help of code.
thank you for replying.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikram Bhati wrote:No its not working.

I understand that you're a starter, but I'm afraid ItDoesntWorkIsUseless (←click).

What happened when you ran it? Did it print anything? Did it give you an Exception?

One thing that might be worth doing is to "play at being the JVM". You run the class, so it calls main(), and main calls show()... so what does show() do...?

Winston
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit surprised that code actually compiles. The reason is that the arguments passed have to match the parameters required by that method. There are however two forms of the main method in the Java Language Specification. Careful perusal of those two types may show a variant which will work, and then it will
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: put the [] as part of the type, not after the identifier name.
String[] args please, rather than String args[]
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it did not compile.
Here is the image Vikram posted earlier
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Actually it did not compile.
. . .
I thought not.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To directly answer the question... the main() method is just like any other static method, so you can call it just like any other static method.

Henry
 
Vikram Bhati
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:By the way: put the [] as part of the type, not after the identifier name.
String[] args please, rather than String args[]


It generates same error after doing this too. I'm simply want to know that can we recall the main method after calling of JVM. That's it
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Henry has answered your question. You can - if you do it correctly.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikram Bhati wrote:It generates same error after doing this too. I'm simply want to know that can we recall the main method after calling of JVM. That's it

And you've already been given the answer. However, even if you get your code to compile, your program will still not work; but the reason has nothing to do with whether you can call main() or not.

Please remember that we're all volunteers here, and our style is to help YOU find the answer, not to spoon-feed it to you.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you a hint how to get rid of the compiler error.
Do you know why you are getting the error? Did you follow the Java Language Specification link I gave you?
 
Vikram Bhati
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, This time it works.
Thanks to all of you

Here is running code :
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is different from what I expected, but
  • 1: Have you worked out why it compiles at all?
  • 2: What happened when you ran that code?
  •  
    Vikram Bhati
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:That is different from what I expected, but
  • 1: Have you worked out why it compiles at all?
  • 2: What happened when you ran that code?


  • When I ran this code earlier then it gives the error (string can't be converted to string[]).

    1. earlier : main("javaranch") i.e wrong
    that should have been : main(arr) and arr is a string type array for example String arr[]={"abc","dad","dae"};
    OR
    what I did now : main(null) that means there is no value in the array and we pass the base address of array which is null.

     
    Bartender
    Posts: 2087
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vikram Bhati wrote:main(null) that means there is no value in the array

    No, it does not mean that there is a null in the array. It means that there is no array at all.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nearly.

    Yes, passing new String[]{"rhubarb", "and", "custard"} would work because it is an array of Strings.
    Passing null worked because it is possible for a String[] reference to be null. Not a good idea passing nulls around however.
    And have you worked out why the other version of main in the JLS would have worked?
    And have you worked out why you obtained that result when you executed that program?
     
    Master Rancher
    Posts: 2045
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can call 'main("some string")' easily without getting the error about String[].

    You could use:

    Greetz,
    Piet
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That was what I thought OP would find.

    But the count < 10 bit takes all the fun out of it
     
    Vikram Bhati
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Piet Souris wrote:
    public static void main(String... args)


    What do you mean by three dots after String (String... args)?
    Is that mean we are not passing string array here?
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And why can you change line 13 to main();
    ?
     
    Piet Souris
    Master Rancher
    Posts: 2045
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah, I admit this is unfortunate. But I copied the program
    without much thinking (or should I say: with the usual amount of thinking..?).

    Greetz,
    Piet
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, the ellipsis operator … takes the arguments passed and converts them to an array. So passing "flup" means the … converts it to a String[] like this:-
    {"flup"}
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For the … operator look in the Java Tutorials and look for the section about arbitrary numbers of arguments.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!