0

I need to create an array of objects (Cars) and then initialise the array to be of a required size (10) within a constructor. So far I have this:

public class Queue {

    private Car [] car;

    public Queue (Car [] car) {
        Car [] car = new Car[10];
    }   
}

Which when I compile says it cannot find the Car symbol. I'm presuming the problem is with the creation of the array as I don't seem to have created the Car properly. Any help would be appreciated <3

3
  • Have you defined car class?? Commented Mar 31, 2013 at 13:30
  • 1
    where is the class Car defined ? Commented Mar 31, 2013 at 13:31
  • 1
    You're declaring a car variable three times: once as a field, once as a parameter to the constructor, and once as a local variable in the constructor. None of them are actually used. Commented Mar 31, 2013 at 13:34

2 Answers 2

2

This is the proper way to create it.

public class Queue {
    private Car [] car;
    public Queue () {
       car = new Car [10];
    }   
}

Provided that you have defined the car class.

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

1 Comment

OP also wants to initialize the array.
0

Create a Car Class and put it in classpath and it will work smoothly .

EDIT[2nd time]: editing my code as per commments below. This shld work

public class Queue {

    private Car [] mycar;

    public Queue (Car [] car) {
        mycar = car;
    }   
}

EDIT:

class Car {

}

4 Comments

Well, except for the fact that the OP's code is wrong and doesn't compile even with the Car class defined.
I have not downvoted, but have you read my comment? Your (well, the OP's) constructor for Queue is completely broken.
@Lokesh car variable in the constructor is redefined that's an error.
@Lokesh still wrong the local variable has shadowed the instance variable. You should use this operator.

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.