To answer the more general question: importing is the preferred way of including external files. In my opinion the include
statement should be only used when nothing else will do as it makes things a bit more difficult to debug if something goes wrong and makes code usually more difficult to read and comprehend. Assaf's description of what import
and include
do is correct.
And then for the more specific problem you seem to have: you're probably trying to do the
testing.init();
right there in the <script>
block -- try putting it in a method. You should only have things like import
statements and member declarations (variables, functions) directly in the script block and statements like this within functions.
You're seeing that error message because when you're calling the init()
method of this object, it hasn't been created yet -- that statement will be executed when the definition of the class that your MXML file represents is loaded; what you want is to have it executed when a particular instance of this class has been created, and you can do that by calling it in the constructor of the class (this is, as far as I know, not possible when you're writing a class using MXML, so read on:) or for example in a handler function for the FlexEvent.CREATION_COMPLETE
(or creationComplete
in terms of MXML tag attributes) event (see the example below.)
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
width="800" height="600"
creationComplete="creationCompleteHandler();"
>
<mx:Script>
<![CDATA[
import lib.Journal;
public var testing:Journal = new Journal();
private function creationCompleteHandler():void
{
testing.init();
}
]]>
</mx:Script>
</mx:Application>