Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4325050
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:12:54+00:00 2026-05-21T09:12:54+00:00

Possible Duplicate: Where is the “proper” place to initialize class variables in AS3 I

  • 0

Possible Duplicate:
Where is the “proper” place to initialize class variables in AS3

I was wondering if anyone knows wether its better to instantiate class on it’s variable declaration or within a constructor? For example, this:

protected var _errorHandler:ErrorHandler = new ErrorHandler();

or this:

protected var _errorHandler:ErrorHandler;

public function someClass() {
_errorHandler = new ErrorHandler();
}

A small point I think, but I want my code to robust and efficient as possible!

Thanks

Chris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:12:55+00:00Added an answer on May 21, 2026 at 9:12 am

    Initialization in the constructor is preferred, for readability–for being able to easily see what gets initialized when. The least readable option would be to mix these, which I can’t recommend.

    There is a third option that you will see AS3 programmers use:

    • No initialization in the variable declarations
    • Empty (or nearly empty) constructor
    • All initialization done in one or more dedicated init() functions

    This approach has two things to offer:

    1. You can easily reset the object for re-use by calling init again
    2. You can get around the limitation that AS3 does not let you overload the constructor like other similar languages (Java/C++/C#). You might want to, for example, be able to initialize a data structure with one or more different types of objects.

    As far as performance goes, I believe your two examples would compile down to the same byte code. The AS3 compiler makes a special class initializer for static declarations that are outside the constructor, but for regular member variables initialized at declaration time, I expect it just moves the initializations to inside the constructor for you. But does it move them ahead or after what is explicitly in the contructor? I don’t remember, which is why I cite readability as a main reason to put everything in the constructor yourself 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why shall I use the “using” keyword to access my base class
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm very confused about
Possible Duplicate: What does “for(;;)” do in C#? what does for (; ; )
Possible Duplicate: replace all “foo” between () Hello, I tried to use regex to
Possible Duplicate: What is the purpose of the expression “new String(…)” in Java? What's
Possible Duplicate: How do I read a “,” as “<br />” in PHP/MySQL? This
Possible Duplicate: Where and why do I have to put “template” and “typename” on
Possible Duplicate: What is the difference between String.Empty and “” What is the difference
Possible Duplicate: Why write <script type=“text/javascript”> when the mime type is set by the
Possible Duplicates: Templated function being reported as “undefined reference” during compilation Why can templates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.