wood burning stoves*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""Cannot find symbol" Compiling Error..." Watch ""Cannot find symbol" Compiling Error..." New topic
Author

"Cannot find symbol" Compiling Error...

Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
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

Joined: Oct 02, 2003
Posts: 11160
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
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

Joined: Oct 27, 2005
Posts: 19653
    
  18

Or change the constructor of B:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
The "X" puts that value (maybe 5) into whichever field of the superclass you would set by writing new A(5);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Correct. Who am I to tell you to use 5? Perhaps you prefer 0. Or 481. Or Integer.MAX_VALUE.
Oscar E Rodriguez
Greenhorn

Joined: Dec 12, 2008
Posts: 22
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

Joined: Jan 28, 2003
Posts: 4167
    
  21

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


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: "Cannot find symbol" Compiling Error...
 
Similar Threads
problem on extending a class declared on the same package!!
Autoboxing, checking is two objects refer to the same object
constructor
help clarify signed, long, int
Problem with protected variable...