Note that there are some explanatory texts on larger screens.

plurals
  1. POhow does self instantiating mechanism/concept works in java?
    text
    copied!<p>I'm new to Java and to OOP, so some things below may sound silly, but...</p> <p>I understand a situation when i have two classes (two different types) and one does whatever it wants with the instance of the other one, like</p> <pre><code>class A { ... } class B { ... public A someMethod() { return new A; } ... } </code></pre> <p>At the point when B's method is declaring some code to work with A, the A type is pretty defined and complete and thus one thing can use/make something with another. That all is pretty logical for me at this point.</p> <p>But I was thinking about the case when a class declaration has a method in it that works with the instance of its own type. How that is possible? I would probably imagine some 'loop', but it's not a loop. How the method can have a code about something that is not 'complete' or completely known at the moment when the code is being written.</p> <p>Sorry if i made some muddled description, I just couldn't find better words to explain this. Absolutely can't fit this concept into my head. Could somebody please explain?</p> <p>UPDATE: i found some snippet that may help you to understand my problem. The code in the class below does not create instance of itself, but it casts the absolutely other object to the its (RectanglePlus') type... </p> <pre><code>public class RectanglePlus implements Relatable { ... // four constructors public RectanglePlus() { origin = new Point(0, 0); } ... // a method required to implement // the Relatable interface public int isLargerThan(Relatable other) { RectanglePlus otherRect = (RectanglePlus)other; if (this.getArea() &lt; otherRect.getArea()) return -1; else if (this.getArea() &gt; otherRect.getArea()) return 1; else return 0; } } </code></pre> <p>Copyright © 2008, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload