{"id":398,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=398"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Objects","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=398","title":{"rendered":"Objects"},"content":{"rendered":"<p><DIV id=PageTitle>Objects<\/DIV><br \/>\n<BLOCKQUOTE><!-- Objects -->A typical Java program creates many objects, which as you know, interact by invoking methods Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.<br \/>\n<P>Here&#8217;s a small program, called <A class=SourceLink href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/java\/javaOO\/examples\/CreateObjectDemo.java\" target=_blank><CODE>CreateObjectDemo<\/CODE><\/A>, that creates three objects: one <A class=SourceLink href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/java\/javaOO\/examples\/Point.java\" target=_blank><CODE>Point<\/CODE><\/A> object and two <A class=SourceLink href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/java\/javaOO\/examples\/Rectangle.java\" target=_blank><CODE>Rectangle<\/CODE><\/A> objects. You will need all three source files to compile this program. <\/P><br \/>\n<BLOCKQUOTE><PRE>public class CreateObjectDemo {<\/p>\n<p>    public static void main(String[] args) {<\/p>\n<p>        \/\/Declare and create a point object<br \/>\n        \/\/and two rectangle objects.<br \/>\n        Point originOne = new Point(23, 94);<br \/>\n        Rectangle rectOne = new Rectangle(originOne, 100, 200);<br \/>\n        Rectangle rectTwo = new Rectangle(50, 100);<\/p>\n<p>        \/\/display rectOne&#8217;s width, height, and area<br \/>\n        System.out.println(&#8220;Width of rectOne: &#8221; +<br \/>\n                rectOne.width);<br \/>\n        System.out.println(&#8220;Height of rectOne: &#8221; +<br \/>\n                rectOne.height);<br \/>\n        System.out.println(&#8220;Area of rectOne: &#8221; + rectOne.getArea());<\/p>\n<p>        \/\/set rectTwo&#8217;s position<br \/>\n        rectTwo.origin = originOne;<\/p>\n<p>        \/\/display rectTwo&#8217;s position<br \/>\n        System.out.println(&#8220;X Position of rectTwo: &#8221;<br \/>\n                + rectTwo.origin.x);<br \/>\n        System.out.println(&#8220;Y Position of rectTwo: &#8221;<br \/>\n                + rectTwo.origin.y);<\/p>\n<p>        \/\/move rectTwo and display its new position<br \/>\n        rectTwo.move(40, 72);<br \/>\n        System.out.println(&#8220;X Position of rectTwo: &#8221;<br \/>\n                + rectTwo.origin.x);<br \/>\n        System.out.println(&#8220;Y Position of rectTwo: &#8221;<br \/>\n                + rectTwo.origin.y);<br \/>\n    }<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE>This program creates, manipulates, and displays information about various objects. Here&#8217;s the output:<br \/>\n<BLOCKQUOTE><PRE>Width of rectOne: 100<br \/>\nHeight of rectOne: 200<br \/>\nArea of rectOne: 20000<br \/>\nX Position of rectTwo: 23<br \/>\nY Position of rectTwo: 94<br \/>\nX Position of rectTwo: 40<br \/>\nY Position of rectTwo: 72<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>The following three sections use the above example to describe the life cycle of an object within a program. From them, you will learn how to write code that creates and uses objects in your own programs. You will also learn how the system cleans up after an object when its life has ended. <\/P><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Objects A typical Java program creates many objects, which as you know, interact by invoking methods Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, &hellip; <a href=\"https:\/\/www.strongd.net\/?p=398\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Objects<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-398","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=398"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}