{"id":56,"date":"2003-12-29T20:04:42","date_gmt":"2003-12-30T02:04:42","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=56"},"modified":"2003-12-29T20:04:42","modified_gmt":"2003-12-30T02:04:42","slug":"java-tidbits","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/56","title":{"rendered":"Java Tidbits"},"content":{"rendered":"<p>Quick, take this true\/false quiz and test your Java(tm) knowledge:<\/p>\n<p>\n1.  Private members can only be accessed by the object which owns them.<\/p>\n<p>\n2.  The contents of a finally block are guaranteed to run.<\/p>\n<p>\nEverybody knows that both of these are true, right?<\/p>\n<p>\nActually each of these is false.  In the first case, this program shows that objects can twiddle with private members of other objects of the same class:<\/p>\n<p><pre>\nclass PrivateMember {\n   private int i; \n   public PrivateMember() { \n      i = 2;\n   }\n   public void printI() {\n      System.out.println(\"i is: \"+i);\n   }\n   public void messWithI(PrivateMember t) {\n      t.i *= 2;\n   }\n   public static void main (String args[]) {\n      PrivateMember sub = new PrivateMember();\n      PrivateMember obj = new PrivateMember();\n      obj.printI();\n      sub.messWithI(obj);\n      obj.printI();\n   }\n}\n<\/pre>\n<p>\nand this program shows that finally blocks don&#8217;t run if the JVM exits:<\/p>\n<p><pre>\nclass TryCatch {\n   public static void main (String args[]) {\n      try {\n         System.out.println(\"in try\");\n         throw new Exception();\n      } catch (Exception e) {\n         System.out.println(\"in catch\");\n         System.exit(0);\n      } finally {\n         System.out.println(\"in finally\");\n      }\n   }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Quick, take this true\/false quiz and test your Java(tm) knowledge: 1. Private members can only be accessed by the object which owns them. 2. The contents of a finally block [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}