From: amk@ecst.csuchico.edu To: mpowell@agilent.com Subject: Re: Parts of instantiation < < Apparently one can instantiate a new instance of a class using < at least 2 approaches, the combined approach: < < Balloon myBalloon = new Balloon(20,50,50); < < or the two-step approach: < < Balloon myBalloon; < myBalloon = new Balloon(20,50,50); < < There seems to be at least 2 things happening here... < < But I can't for the life of me understand why "Balloon myBalloon" < is needed. I guess it is simply saying up front what "myBalloon" < is. It is destined to be an instance of Balloon. Yes? < < But is that not obvious to the compiler in the second line alone? < < myBalloon = new Balloon(20,50,50); < < Everything it needs to know seems to be there.... a new instance < of Balloon will be created and called "myBalloon", and will have < these initial values... < < What is the "Balloon myBalloon;" portion of the code called? Why < is it there? Note that in both cases the *declaration* part Balloon myBalloon is needed. An instance is "declared" to be one of a class type. Declarations can be checked at compile time. If an instance is declared to be something, then this thing must be available. SO, the class must be in the imports section of your code (unless it is in java.lang or in the same package as the class using it ... which is what is happening in this case) On the other hand, instances are not created until run time. So if you did *not* have this declaration part, the system would give you errors that *could* have been told to you at compilation and now need to stop you at runtime. Not a good idea. Also if you did not have the declaration requirement, the compiler would have to check every use of new. Again, not a good idea. < < The reason for all of these questions comes from lab 4, where < the line: < < ComputeFigure theCurrentApplet; < < This is simply "Balloon myBalloon;" again, yes? < < It appears without the creation of an instance (no "new") following < it. My quest is to understand what this line of code means... < what it is doing. What does it do for the applet when "theCurrentApplet" < is never actually instantiated? Or is it somehow instantiated by < some implied means? In the code you see class TheMouseHandler extends MouseAdapter implements MouseMotionListener { ComputeFigure theCurrentApplet; public TheMouseHandler(ComputeFigure x) { theCurrentApplet=x; } Here theCurrentApplet is not instantiated since it already *was* instatiated as a ComputeFigure in another class that then passes it as a variable to the TheMouseHandler class See the line MouseManager=new TheMouseHandler(this); in the ComputeFigure class. Since it is passing "this" as a parameter it means that it is passing an instance of ComputeFigure (since "this" is a ComputeFigure) All in all, rather beautiful :-) Anne