{"id":139,"date":"2007-08-23T22:09:41","date_gmt":"2007-08-24T02:09:41","guid":{"rendered":"http:\/\/nule.org\/wp\/?page_id=139"},"modified":"2007-08-23T22:09:41","modified_gmt":"2007-08-24T02:09:41","slug":"04-classes","status":"publish","type":"page","link":"https:\/\/nule.org\/?page_id=139","title":{"rendered":"04 &#8211; Classes"},"content":{"rendered":"<p>Classes in Java represent the way in which we organize our code.  Each class should represent a logical or functional unit of code and a single program can consist of one or many classes.  It&#8217;s important to code your classes as generically as possible so that they may be reused by other programs.<\/p>\n<p>For the most part classes have a few simple rules to follow to be valid.  Your class name should be the same as the name of the file that contains it.  For example if you have a class name MyClass it should be in a file called MyClass.java.  It is recommended that class names should be uppercase.  The most basic Java class file looks like this:<\/p>\n<p><code><br \/>\n\/\/ ExampleClass.java<br \/>\npublic class ExampleClass {<br \/>\n}<br \/>\n<\/code><\/p>\n<p>As we mentioned before a class must have a main method with the exact signature above to be a program, but a program often consists of many classes put together, and only the main class needs to have the main method.<\/p>\n<p>Classes often have another special method called the constructor.  It is called whenever that class is instantiated, and all classes have them whether you&#8217;ve defined one or not (it&#8217;s inherited from the Object class, but you&#8217;ll learn more about that later).  Because of this often a blank one is put into a class when it&#8217;s created specifically to avoid confusion.<\/p>\n<p><code><br \/>\n\/\/ ExampleClass.java<br \/>\npublic class ExampleClass {<br \/>\n&nbsp;&nbsp;public ExampleClass() {<br \/>\n&nbsp;&nbsp;}<br \/>\n}<br \/>\n<\/code><\/p>\n<p>You can see that the constructor looks similar to other methods, but it has no return type.  Effectively it returns an instance of the class you are creating, so a return type would be inappropriate here.  The constructor can take arguments, however, which could come in handy for setting initial conditions for a newly created object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Classes in Java represent the way in which we organize our code. Each class should represent a logical or functional unit of code and a single program can consist of one or many classes. It&#8217;s important to code your classes as generically as possible so that they may be reused by other programs. For the [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"parent":129,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-139","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nule.org\/index.php?rest_route=\/wp\/v2\/pages\/139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nule.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nule.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nule.org\/index.php?rest_route=\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/nule.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=139"}],"version-history":[{"count":0,"href":"https:\/\/nule.org\/index.php?rest_route=\/wp\/v2\/pages\/139\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/nule.org\/index.php?rest_route=\/wp\/v2\/pages\/129"}],"wp:attachment":[{"href":"https:\/\/nule.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}