{"id":10992,"date":"2015-06-14T17:46:22","date_gmt":"2015-06-14T17:46:22","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=10992"},"modified":"2017-04-26T15:34:57","modified_gmt":"2017-04-26T15:34:57","slug":"java-programming-anonymous-class","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/java-programming-anonymous-class\/","title":{"rendered":"Java programming:  anonymous class"},"content":{"rendered":"<h1>The tutor brings up a beloved feature of Java: the anonymous class.<\/h1>\n<p>Reading about Java, or reading Java code, you might often encounter an anonymous class.  It&#8217;s a class that is created with the <span style=\"font-family:monospace\">new<\/span> operator, but not given a name.  Often, one of its methods is called simultaneously &#8211; the very reason for its creation:<br \/>\n<span style=\"font-family:monospace;color:olive\"><br \/>\nnew Aclass().methodNeeded();<br \/>\n<\/span><\/p>\n<p>Here&#8217;s an example.  First, the code for the class that will be called anonymously:<br \/>\n<span style=\"font-family:monospace;color:olive\"><br \/>\nclass Anon0{<br \/>\npublic void reply(){<br \/>\nSystem.out.println(&#8220;Yes, I&#8217;m here.&#8221;);<br \/>\n}<br \/>\n}<br \/>\n<\/span><\/p>\n<p>Now, the application class (the one with the <span style=\"font-family:monospace\">main()<\/span> method):<br \/>\n<span style=\"font-family:monospace;color:olive\"><br \/>\nclass Driver{<br \/>\npublic static void main(String[] args){<br \/>\nnew Anon0().reply();<br \/>\n}<br \/>\n}<br \/>\n<\/span><\/p>\n<p>After compiling and running (see how in my article <a href=\"?p=10882\">here<\/a>),  you are hopefully rewarded with the greeting, from the anonymous class,<br \/>\n<span style=\"font-family:monospace\"><br \/>\nYes, I&#8217;m here.<br \/>\n<\/span><\/p>\n<p>Hopefully you can have some fun with anonymous classes:)<\/p>\n<p>Source:<\/p>\n<p>Niemeyer, Patrick and Jonathan Knudsen.  <u>Learning Java<\/u>, third ed.  Sebastopol:  O&#8217;Reilly Media, Inc., 2005.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor brings up a beloved feature of Java: the anonymous class. Reading about Java, or reading Java code, you might often encounter an anonymous class. It&#8217;s a class that is created with the new operator, but not given a &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/java-programming-anonymous-class\/\"> <span class=\"screen-reader-text\">Java programming:  anonymous class<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[875,602],"class_list":["post-10992","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-anonymous-class","tag-java-programming"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=10992"}],"version-history":[{"count":13,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10992\/revisions"}],"predecessor-version":[{"id":21184,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10992\/revisions\/21184"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=10992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=10992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=10992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}