{"id":244,"date":"2015-06-19T13:54:28","date_gmt":"2015-06-19T03:54:28","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=244"},"modified":"2015-06-19T13:54:28","modified_gmt":"2015-06-19T03:54:28","slug":"fix-cannot-static-reference-non-static-method","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/fix-cannot-static-reference-non-static-method\/","title":{"rendered":"How to fix “Cannot make a static reference to the non-static method”"},"content":{"rendered":"

Static methods cannot call non-static methods. An instance of the class is required to call its methods and static methods are not accociated with an instance (they are class methods). To fix it you have a few choices depending on your exact needs.<\/p>\n

\r\n\r\n\/**\r\n*  Will not compile\r\n*\/\r\n\r\npublic class StaticReferenceToNonStatic\r\n{\r\n   public static void myMethod()\r\n   {\r\n      \/\/ Cannot make a static reference\r\n      \/\/ to the non-static method\r\n      myNonStaticMethod(); \r\n   }\r\n\r\n   public void myNonStaticMethod()\r\n   {\r\n   }\r\n}\r\n\r\n\/**\r\n* you can make your method non-static\r\n*\/\r\n\r\npublic class MyClass\r\n{\r\n   public void myMethod()\r\n   {\r\n      myNonStaticMethod(); \r\n   }\r\n\r\n   public void myNonStaticMethod()\r\n   {\r\n   }\r\n}\r\n\r\n\/**\r\n*  you can provide an instance of the \r\n*  class to your static method for it \r\n*  to access methods from\r\n*\/\r\n\r\npublic class MyClass\r\n{\r\n   public static void myStaticMethod(MyClass o)\r\n   {\r\n      o.myNonStaticMethod(); \r\n   }\r\n\r\n   public void myNonStaticMethod()\r\n   {\r\n   }\r\n}\r\n\r\n\/**\r\n*  you can make the method static\r\n*\/\r\n\r\npublic class MyClass\r\n{\r\n   public static void myMethod()\r\n   {\r\n      f(); \r\n   }\r\n\r\n   public static void f()\r\n   {\r\n   }\r\n} \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Static methods cannot call non-static methods. An instance of the class is required to call its methods and static methods are not accociated with an instance (they are class methods). To fix it you have a few choices depending on your exact needs.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[68,67],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-3W","jetpack-related-posts":[{"id":235,"url":"https:\/\/learn-java-by-example.com\/java\/check-java-string-integer\/","url_meta":{"origin":244,"position":0},"title":"How to check if a Java String is an integer?","date":"June 5, 2015","format":false,"excerpt":"The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":133,"url":"https:\/\/learn-java-by-example.com\/java\/fibonnaci-number\/","url_meta":{"origin":244,"position":1},"title":"Fibonnaci Number","date":"December 3, 2010","format":false,"excerpt":"Generating Fibonacci numbers is an assignment often given to Java students when they are being introduced to recursion. Recursion is programming technique that involves a method calling itself to solve a problem. When implementing a recursive solution we look for two things: A base case that returns a value without\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":214,"url":"https:\/\/learn-java-by-example.com\/java\/convert-inches-centimetres\/","url_meta":{"origin":244,"position":2},"title":"Convert Inches to Centimetres","date":"October 14, 2011","format":false,"excerpt":"The next example was kindly shared by one of our members. She had a Java Swing assignment to create a Swing GUI for converting Inches to Centimetres and had no idea where to start. We helped her break down the problem and come up with a solution. The resulting code\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":238,"url":"https:\/\/learn-java-by-example.com\/java\/selection-sort\/","url_meta":{"origin":244,"position":3},"title":"Selection Sort","date":"June 19, 2015","format":false,"excerpt":"When you are learning Java you are very likely going to be asked to write a program to sort values. We will start with one of the simpler sort algorithms, the Selection Sort (also known as an Exchange Sort). It's not a particularly efficient algorithm and really only suitable for\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":56,"url":"https:\/\/learn-java-by-example.com\/java\/monthly-payment-calculator\/","url_meta":{"origin":244,"position":4},"title":"Monthly Payment Calculator","date":"June 2, 2010","format":false,"excerpt":"This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user. User input in this example is taken from the console (standard\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":300,"url":"https:\/\/learn-java-by-example.com\/java\/control-decimal-places-displayed-jtable-column\/","url_meta":{"origin":244,"position":5},"title":"How to control decimal places displayed in JTable column?","date":"September 10, 2015","format":false,"excerpt":"Rendering of table cells is handled by instances of TableCellRenderer. By default JTable uses a DefaultTableCellRenderer to render all of its cells. To control the number of decimal places used we just need to subclass DefaultTableCellRenderer and format the cell double value before passing the (formatted) value to to the\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/244"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":1,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":245,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/244\/revisions\/245"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}