Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Cannot find symbol" Compiling Error...

 
Oscar E Rodriguez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been racking my brain with this since last night and after multiple attempts and leafing through 2 books I can't seem to get this...

I have the following:


and I am getting the following error when I compile:

Exam9.java:8: cannot find symbol
symbol : constructor A()
location: class A
B() {System.out.println(" class B");}
^

I've tried employing "this" and "super" with no success...I must be looking at this error the wrong way.

Any help would be GREATLY appreciated.
Thank you in advance.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared your own constructor that takes an int. If you define your own constructor with any signature, java will not create the no-arg version by default.

Then, in your class B, you make a call for the no-arg constructor of a 'B' object. Since a B IS-AN A, you are effectively calling the no-arg constructor for 'A', which doesn't exist. That is what the compiler is telling you.

You can fix this by defining a no-arg constructor for your 'A' class.
 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first line in each constructor is either a call to a super constructor or a call to another constructor of the same class. If this is not written explicitly by the programmer, the compiler implicitly will put a call to "super()". Which what the compiler does to your B() constructor.

However, super() in this case is A(), which does not exist.

To solve,
just put a A() constructor in class A.

Hope that solves it.
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or change the constructor of B:
 
Oscar E Rodriguez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Or change the constructor of B:


Thank you, Rob, again!

Just a clarification...
what does the X (constant number) do in this code?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "X" puts that value (maybe 5) into whichever field of the superclass you would set by writing new A(5);
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. Who am I to tell you to use 5? Perhaps you prefer 0. Or 481. Or Integer.MAX_VALUE.
 
Oscar E Rodriguez
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Rob and Campbell, for the feedback.

I understand now the need for a value to be placed in for "X", but does the value I choose to put in there affect how the code executes?

I've tired different values for "X" and executed the main and haven't noticed any diffence in the output.

Thanks, again
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So look how you treat the integer in A(int a). What do you do with it? How would you then expect passing different values into that constructor would affect how the application runs?


Originally posted by Oscar E Rodriguez:
Thank you, Rob and Campbell, for the feedback.

I understand now the need for a value to be placed in for "X", but does the value I choose to put in there affect how the code executes?

I've tired different values for "X" and executed the main and haven't noticed any diffence in the output.

Thanks, again
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change to thisThe problem is that you are not using the X argument. The second version should correct your original errors, and uses that argument.

You weren't using the constructor properly; the idea of a constructor is to set the initial values of all the fields. You left "x" alone, so it will default to 0. Don't have a field of the same name in a subclass; that can cause no end of confusion later on.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic