2

This is how I'm trying to do it:

interface a{} 
class b implements a{
    a[] array; 
    new b(){
        array={ new aImplementer(), new aImplementer(), new aImplementer()}; 
    } 
} 

Why can't I do this? Am I just doing it wrong? Right now the error I'm getting is an illegal start of expression error at the { part of array={

2 Answers 2

8

Your syntax is off. I believe you want something like this (Final revision--actually tested this time--then redone after my changes were overridden.)

interface A{} 

class AImplementer implements A{};

class B { 
    A[] array; 
    B(){  
        array=new A[]{ new AImplementer(), new AImplementer(), new AImplementer()}; 
    } 
 }

There, guaranteed to compile or twice your money back :)

Also threw in "classes should start with an upper case letter" for no charge.

Sign up to request clarification or add additional context in comments.

5 Comments

this gives me an <identifier> expected error right after the new
is it because you didn't define aImplementor? There, I think I put what you were trying for. Does that work for you?
Also note that if you really intended to use b as "aImplementer" then your logic is also wrong because b will recurse in the constructor and create b's until you run out of memory.
b is not an aImplementor, it is a collection of aImplementors
In that case, b should not implement a, and my answer above is correct.
1

You can only assign an array to an array literal (I'm not sure what you call these beasts) at declaration of the variable. So this may be OK

// array literal assigned at variable declaration
a[] array = { new aImplementer(), new aImplementer(), new aImplementer()};  
new b(){

} 

But what you have where you declare it first and then assign it at a different spot isn't OK. Why? I'm not sure other than that's how it is in the JLS.

Edit: Updated compiled/tested code:

interface A {
}

class AImplementer implements A {
};

class B {
   A[] array = {new AImplementer(), new AImplementer(), new AImplementer()};

   B() {

   }
}

6 Comments

I think you still need the second "new" as in: a[] array = new a[]{blah} (but it wasn't me who downvoted you--that was some fly-by).
@Bill: no, the code compiles fine without the new. Try it yourself to be sure. You need the new a[] only if initiating the array when not at declaration. Either of our code will work. +1 for your great example. /Pete
@Bill K +1 for nicer code. BTW: Those codes both compile fine String[] str1={"", "", ""}; String[] str2=new String[]{"", "", ""}; @Hovercraft Full Of Eels +1 for being first giving 'the same' answer and being down-voted, why hell knows?
@Boro: thanks, but down votes don't really bug me as I've got as many points as I need. You can't really redeem them for nuthin' :)
You're right--didn't ever notice that differentiation, good to know. +1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.