{"id":1771,"date":"2016-09-26T04:10:50","date_gmt":"2016-09-26T04:10:50","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1771"},"modified":"2016-09-26T04:25:55","modified_gmt":"2016-09-26T04:25:55","slug":"java-main-mehod","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/java-main-mehod\/","title":{"rendered":"Java main() Mehod"},"content":{"rendered":"<p><strong>Why main() method is Static<\/strong><\/p>\n<ol>\n<li>the main function is where a program starts execution. It is responsible for the high-level organization of the program&#8217;s functionality, and typically has access to the command arguments given to the program when it was executed.<\/li>\n<li>main() is static because at that point in the application&#8217;s lifecycle, the application stack is procedural in nature due to there being no objects yet instantiated.It&#8217;s a clean slate. Your application is running at this point, even without any objects being declared (remember, there&#8217;s procedural AND OO coding patterns). You, as the developer, turn the application into an object-oriented solution by creating instances of your objects and depending upon the code compiled within.<\/li>\n<li>static allows main() to be called before an object of the class has been created. This is neccesary because main() is called by the JVM before any objects are made. Since it is static it can be directly invoked via the class.<\/li>\n<li>The method is static because there would be ambiguity: which constructor should be called? Especially if your class looks like this:\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class JavaClass\r\n{\r\n  protected JavaClass(int x){}\r\n\r\n  public void main(String&#x5B;] args)\r\n  {\r\n  }\r\n}\r\n<\/pre>\n<p>If the JVM call new JavaClass(int)? What should it pass for x?<\/li>\n<\/ol>\n<p><strong>What would be the Output<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Test \r\n{\r\n    static\r\n    {\r\n      System.out.println(&quot;Hi static 1&quot;);        \r\n    }\r\n\r\n    public static void main(String&#x5B;] args) \r\n    {\r\n        System.out.println(&quot;Hi main&quot;);\r\n    }\r\n\r\n    static\r\n    {\r\n        System.out.println(&quot;Hi static 2&quot;);        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nHi static 1\r\nHi static 2\r\nHi main\r\n<\/pre>\n<p><strong>What happens when you run a class without main() method<\/strong><br \/>\nSay, file Demo.java contains source code as<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Demo{ \r\n\r\n}\r\n<\/pre>\n<p>when this code is compiled as it&#8217;s compile successfully as<\/p>\n<pre>\r\n$javac Demo.java\r\n<\/pre>\n<p>but when it&#8217;s executed as <\/p>\n<pre>\r\n$java Demo\r\n<\/pre>\n<p>then it shows an exceptional error <\/p>\n<pre>\r\nMain method not found in class Demo, please define the main method as: public static void main(String[] args)\r\n<\/pre>\n<p>so compiler is not responsible to check whether main() method is present or not. JVM is responsible for it. JVM check for main() method with prototype as <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(Strings&#x5B;] args)\r\n<\/pre>\n<p><strong>Why JVM search main() method? Is it possible to change main() method into any other method main_hero()?<\/strong><br \/>\nJVM is instructed to find main() method from inside JVM. Yes, it&#8217;s possible to change main() method into main_hero() method but inside JVM you must have to instruct to search main_hero() method.<\/p>\n<p><strong>Why public?<\/strong><br \/>\n call from anywhere public is used.<\/p>\n<p><strong>Why void?<\/strong><br \/>\nJVM is going to call main() method but what can JVM do with return value if main() method return.<\/p>\n<p><strong>Can we declare main() method as private or protected or with no access modifier?<\/strong><br \/>\nNo, main() method must be public. You can\u2019t define main() method as private or protected or with no access modifier. This is because to make the main() method accessible to JVM. <em>If you define main() method other than public, compilation will be successful but you will get run time error as no main method found<\/em>.<\/p>\n<p><strong>Can We Declare main() Method As non-static?<\/strong><br \/>\nNo, main() method must be declared as static so that JVM can call main() method without instantiating it\u2019s class. If you remove \u2018static\u2019 from main() method signature, <em>compilation will be successful but program fails at run time.<\/em><\/p>\n<p><strong>Most of the Errors incase of change of main() method are runtime errors<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why main() method is Static the main function is where a program starts execution. It is responsible for the high-level organization of the program&#8217;s functionality, and typically has access to the command arguments given to the program when it was executed. main() is static because at that point in the application&#8217;s lifecycle, the application stack&hellip; <a href=\"https:\/\/codethataint.com\/blog\/java-main-mehod\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[193],"tags":[],"class_list":["post-1771","post","type-post","status-publish","format-standard","hentry","category-interview-questions-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1771"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1771\/revisions"}],"predecessor-version":[{"id":1776,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1771\/revisions\/1776"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}