{"id":10200,"date":"2015-05-05T19:05:29","date_gmt":"2015-05-05T19:05:29","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=10200"},"modified":"2015-05-05T19:05:29","modified_gmt":"2015-05-05T19:05:29","slug":"java-using-classes-part-0","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/java-using-classes-part-0\/","title":{"rendered":"Java:  using classes, part 0"},"content":{"rendered":"<h1>To an academic, Java&#8217;s elegance is delicious. \u00a0The tutor begins coverage of how to use classes.<\/h1>\n<p>In Java, a class is basically a more complex evolution of a type. \u00a0A class can house many variables, but also contain its own functions, which Java calls methods. \u00a0A programmer can use predefined classes provided by Java, by rarely (in my experience) can a problem be solved using predefined classes alone.<\/p>\n<p>A programmer might find themselves in a situation where they need a certain method that a predefined class already contains. \u00a0In my experience, such is often the case: \u00a0you want access to a certain method, so you create an instance of the class that contains it. Then you send your arguments to its method whose functionality you want.<\/p>\n<p>There are five points to be aware of in this context:<\/p>\n<p>1) \u00a0A method is recognizable because it&#8217;s followed by brackets: eg, <span style=\"font-family:monospace\">method1()<\/span><\/p>\n<p>2)  Java is case sensitive; to it, T1 and t1 are not related at all.<\/p>\n<p>3)  Convention says that the title of a Java class begins with a capital letter, but an instance of the class begins with lower case.  Furthermore, a method begins with lower case.<\/p>\n<p>4)  An instance of a class is an embodiment of its definition.  An instance can be created with the <span style=\"font-family:monospace\">new<\/span> operator.<\/p>\n<p>5)  Generally, you need an instance of a class in order to access its methods.<\/p>\n<p>Let&#8217;s imagine, for instance, you want the method <em>m1()<\/em> contained in class <em>T1<\/em>.  What you&#8217;ll likely do is set up an instance of <em>T1<\/em>, here calling it <em>myT1<\/em>:<\/p>\n<p><span style=\"font-family:monospace\">T1 myT1 = new T1(); <span style=\"color:blue\">\/\/Creating an instance is seen as a method.<\/span><\/span><\/p>\n<p>Let&#8217;s imagine you want to send the arguments <em>arg1<\/em> and <em>arg2<\/em> to method <em>m1()<\/em> in <em>myT1<\/em>, then store the result in the variable <em>res<\/em>.  What follows shows how you might do so.  Note that to access the method <em>m1()<\/em> in <em>myT1<\/em>,   you do so using the period . operator:<\/p>\n<p><span style=\"font-family:monospace\">res=myT1.m1(arg1,arg2);<span style=\"color:blue\">\/\/ the . tells Java m1() is from myT1<\/span><\/span><\/p>\n<p>In coming posts I&#8217;ll much more discuss using classes and writing new ones.<\/p>\n<p>HTH:)<\/p>\n<p>Source:<\/p>\n<p>Niemeyer, Patrick and Jonathan Knudsen.  <u>Learning Java<\/u>.  Sebastopol, CA:  O&#8217;Reilly &nbsp;&nbsp;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>To an academic, Java&#8217;s elegance is delicious. \u00a0The tutor begins coverage of how to use classes. In Java, a class is basically a more complex evolution of a type. \u00a0A class can house many variables, but also contain its own &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/java-using-classes-part-0\/\"> <span class=\"screen-reader-text\">Java:  using classes, part 0<\/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":[763,761,766,765,764,514,515,762],"class_list":["post-10200","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-creating-an-instance-of-a-class","tag-java","tag-java-class","tag-method","tag-new-operator","tag-object-oriented-programming","tag-oop","tag-using-classes"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10200","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=10200"}],"version-history":[{"count":30,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10200\/revisions"}],"predecessor-version":[{"id":10230,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10200\/revisions\/10230"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=10200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=10200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=10200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}