7 Replies - 799 Views - Last Post: 24 January 2011 - 12:39 PM Rate Topic: -----

#1 williamwirthtorres  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-January 11

cannot find symbol - variable

Posted 24 January 2011 - 09:07 AM

I am receiving the error message: "cannot find symbol - variable dalmatian" when I try to create an array using the shortcut. What am I doing wrong or forgetting to do?

I have an abstract superclass (Animal) and three subclasses (Dog, Horse, Elephant). In a new class (AnimalTest) I am trying to create an array with 3 assigned values. AnimalTest is in the same package.

So far my code is:
public class AnimalTest{
    
    public static void main( String[] args ){
        Animal[] ar = { dalmatian, arabian, indian };
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: cannot find symbol - variable

#2 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: cannot find symbol - variable

Posted 24 January 2011 - 09:10 AM

Where are dalmatian, arabian and indian declared/defined?
In the current snippet, they aren't.
Was This Post Helpful? 0
  • +
  • -

#3 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: cannot find symbol - variable

Posted 24 January 2011 - 10:05 AM

You need to make a new instance of these classes in order to make an array of them

Animal x = new Animal("sadger"); //Or whatever goes in your constructor


Which you can assign in a loop or individually.

This post has been edited by westforduk: 24 January 2011 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

#4 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: cannot find symbol - variable

Posted 24 January 2011 - 10:24 AM

One defines objects like Class name = new Class(constructor method's parameters);
For this to work you would have to have 3 individual class declarations:

Dog dalmation = new Dog();
Horse arabian = new Horse();
Elephant indian = new Elephant();


if I used the right sub classes. If they aren't created the program won't find them. And yes the array of type Animal will hold them as they all extend Animal.

This post has been edited by TFoSSDQ: 24 January 2011 - 10:27 AM

Was This Post Helpful? 1
  • +
  • -

#5 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: cannot find symbol - variable

Posted 24 January 2011 - 12:15 PM

If you want to stick to the short declaration, the following also works:
Animal[] ar = {new Dalmatian(), new Arabian(), new Indian};


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10575
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: cannot find symbol - variable

Posted 24 January 2011 - 12:16 PM

Don't forget the constructor parentheses for new Indian(). :)
Was This Post Helpful? 1
  • +
  • -

#7 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: cannot find symbol - variable

Posted 24 January 2011 - 12:25 PM

View Postmacosxnerd101, on 24 January 2011 - 12:16 PM, said:

Don't forget the constructor parentheses for new Indian(). :)


Oops, sorry (thanks macosxnerd101).


Animal[] ar = {new Dalmatian(), new Arabian(), new Indian()};


Now it's correct.
Was This Post Helpful? 1
  • +
  • -

#8 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: cannot find symbol - variable

Posted 24 January 2011 - 12:39 PM

View Postn8schatten, on 24 January 2011 - 12:25 PM, said:

View Postmacosxnerd101, on 24 January 2011 - 12:16 PM, said:

Don't forget the constructor parentheses for new Indian(). :)


Oops, sorry (thanks macosxnerd101).


Animal[] ar = {new Dalmatian(), new Arabian(), new Indian()};


Now it's correct.


actually, it's Animal[] ar = {new Dog(), new Horse(), new Elephant()};
because the subclasses are Dog, Horse, and Elephant :smile2:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1