initialize array elements during declaration

Discussion in 'Java' started by hyena, Jan 24, 2005.

  1. hyena

    hyena Guest


    Just want to know how to initialize all the elements using construction
    function in an array when I declare it.

    I defined a class like

    class A {

    //do something here


    in another class, I declare

    A[] test = new A[2];

    I want to pass some parameter at the same time, but i have no idea of where
    shall i initialize the elements in the array. Any tips?

    thanks in advance!

    hyena, Jan 24, 2005
    1. Advertisements

  2. When the JRE creates your array of A objects, the default behavior is to
    set each element in the array to null. There is no way I know of to
    auto-initialize each element of the array to be a non-null object.

    You could address it this way:

    class B
    private A[] test = new A[2];

    * constructor
    public B(String param1, String param2)
    for (int idx=0; idx<test.length; idx++)
    test[idx] = new A(param1,param2);


    Assuming that the params you pass into your container class (B) are the
    same ones that A uses in its constructor.

    Collin VanDyck, Jan 24, 2005
    1. Advertisements

  3. hyena

    DR Guest

    A[] test = new A[]
    new A(...),
    new A(...),

    hyena a écrit:
    DR, Jan 24, 2005
  4. hyena

    hyena Guest

    Thanks you guys very much!
    hyena, Jan 24, 2005
  5. Note that your array declaration (above) declares the array, but doesn't
    create any instances of A [all elements of array test are null].
    Perhaps you want something like this:

    A[] test = new A[] {new A("foo", "bar"),
    new A("forty-two", "what's six times seven?")};

    John Bollinger
    John C. Bollinger, Jan 24, 2005
  6. hyena

    Bob Guest

    Don't forget the semicolon to end the statement.

    (Taking the 310-035 exam on Wednesday, so I'm on a syntax safari hunt.)
    Bob, Jan 24, 2005
  7. Congrats on taking the exam, and please dont take the following as any
    kind of insult to yourself....

    But I can't stand exams or interview tests which test the persons
    ability to read and write code, identifying or not producing syntax errors.

    Twice in recent years I've not answered interview questions of this
    sort, instead I've made it quiet clear to the employer that testing a
    person for this ability is a waste of their time and mine.

    Its a job for compilers.

    I've created the Java tests for my current employer and there isn't a
    single syntax problem to solve. Instead there's questions on general OO
    approaches, Patterns and several on the mechanics of Java (i.e.
    exception handling, Interfaces vs Abstract Classes, Inner classes etc).

    *Phew* rant over...I'll get my coat...
    Andrew McDonagh, Jan 24, 2005
  8. hyena

    DR Guest

    there are also the "..." that the compiler is likely not to appreciate :)

    "Imagination is more important than knowledge" -- Albert Einstein.

    Bob a écrit:
    DR, Jan 24, 2005
  9. hyena

    Ryan Stewart Guest

    On one hand, I can agree with you. On the other, if a job requires the ability
    to program in Java, I think you have to at least make sure someone knows basic
    Java syntax, like maybe make sure he or she can write a Hello World program
    without any help. Sun's reference compiler typically has excellent error
    messages, yes. Not all compilers in all languages have that, and regardless, it
    can be very, *very* beneficial to be able to look over a piece of code and say,
    "Here's what's wrong with it..." without having to use anything else.
    Ryan Stewart, Jan 24, 2005
  10. Yes in other languages it can be beneficial, but in Java, the compilers
    save us from this. therefore I'd rather the candidate knows about
    technology e.g. SWT, Swing, CORBA, TDDing Mock Objects etc.

    They still have to write simple mini apps for the tests, but we make it
    clear that syntax errors will be ignored.

    For instance, one question is to write a thread-safe singleton. This
    test will help identify whether the candidate knows about Threading, The
    Singleton Pattern, how to implement a singleton in Java with

    How they choose to implement it with regards to the varying
    synchronisation mechanism available and how they instantiate the
    singleton object is far more interesting to us, than whether they missed
    a semi-colon, a '{' or a return type, etc.
    Andrew McDonagh, Jan 24, 2005
  11. hyena

    Bob Guest

    I quite agree that not hiring someone on the strength of a missing
    semicolon would be madness. But I have to say that, reading up on what I
    need to know to pass the Programmer (syntax and core features) exam, it
    has taught me how much I did not learn about Java in university. A lot
    of things have been made clear, not just syntax snags. So I'm glad I've
    studied it all. I just hope I don't get fazed on the day, and score too
    low to pass.
    Well, the syntax and behaviour of exception handling, assertions,
    interfaces, abstract classes, inner classes (regular, anonymous,
    method-local, and static nested) are all tested, and these are things I
    would have cluelessly assumed I knew how to do. Now I think I can just
    about do them all inside out. So I feel I've learned a lot.

    Plus, if I pass the exam, I hope to then learn some OO theory and
    practice, and take the Sun Java Developer exam. Do you have a book you
    recommend for learning what can't be taught from a syntax diagram?
    Bob, Jan 24, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.