{"id":191,"date":"2020-12-07T12:00:00","date_gmt":"2020-12-07T12:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=191"},"modified":"2020-12-26T14:16:39","modified_gmt":"2020-12-26T19:16:39","slug":"calling-a-nonstatic-method-from-main","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/calling-a-nonstatic-method-from-main\/","title":{"rendered":"Calling a nonstatic method from main"},"content":{"rendered":"\n<p>A static method is a class method, rather than an object method. So static methods cannot access not static methods without instantiating their objects.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Test {\n   private double income = 5000;\n   private double expenses = 7000;\n\n   public static void main (String&#91;] args) {&nbsp;&nbsp;&nbsp;\n      calculateBalance();\n   }\n\n   private void calculateBalance() {\n      System.out.println(this.income - this.expenses);\n   }\n}<\/code><\/pre>\n\n\n\n<p>This code would generate the following error message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Cannot make a static reference to the nonstatic method calculateBalance()<\/code><\/pre>\n\n\n\n<p>Since main() is static, it cannot call the nonstatic method calculateBalance(). Another mistake new Java programmer make is that they try to use &#8220;this&#8221; keyword.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Test{\n   private double income = 5000;\n   private double expenses = 7000;\n\n   public static void main(String&#91;] args) {&nbsp;&nbsp;&nbsp;\n      this.calculateBalance();\n   }\n   private void calculateBalance() {\n      System.out.println(this.income - this.expenses);\n   }\n}<\/code><\/pre>\n\n\n\n<p>The program generates the following error.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Test{\n   private double income;\n   private double expenses;\n   \n   public static void main(String&#91;] args) {\n      Test t = new Test();\n      t.calculateBalance();\n   }\n\n   Test() {\n      this.income = 5000;\n      this.expenses = 7000;\n   }\n\n   private void calculateBalance() {\n      System.out.println(this.income - this.expenses);\n   }\n}<\/code><\/pre>\n\n\n\n<p>This program compiles and produces the output -2000. To access a nonstatic method from a static method, you need to instantiate its object first. &#8220;this&#8221; keyword cannot be used in static context. &#8220;this&#8221; is an object&#8217;s internal reference. Since a static method is a class method, it does not have an internal object reference.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A static method is a class method, rather than an object method. So static methods cannot access not static methods without instantiating their objects. This code would generate the following error message. Since main() is static, it cannot call the nonstatic method calculateBalance(). Another mistake new Java programmer make is that they try to use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":413,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38],"class_list":["post-191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":2,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":414,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/191\/revisions\/414"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/413"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}