aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor not found error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor not found error" Watch "Constructor not found error" New topic
Author

Constructor not found error

Milind Chaudhary
Greenhorn

Joined: Jul 09, 2010
Posts: 20
I have the following simple program which gives a "Constructor not found" error on compiling

CODE:


Please point out what is the problem with the code.. Thanks in advance..
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Before the reference variable f is initialized, you are accessing the msg property on it by passing it as a parameter in the constructor (1st line of main method). That's not allowed...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Milind Chaudhary
Greenhorn

Joined: Jul 09, 2010
Posts: 20
Thanks for your answer...
Milind Chaudhary
Greenhorn

Joined: Jul 09, 2010
Posts: 20
Just another question.. if I declare the variable msg as static and have the following code



Then where is the problem.. ?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
The code below, which I believe is the same as yours (with the static msg)
shows a compiler error in NetBeans 6.9, but it still runs fine. The error is:
"...cannot find [default] constructor Fruit()..." After puzzling a while I think
this is the cause. Since Apple has no String constructor (just the default), and
Fruit has no default constructor (only String), there is no way to initialize an
Apple object. Clever compiler - ey? Jim ... ...


BEE MBA PMP SCJP-6
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Because the super class Fruit does not have a default constructor (one with no parameters), the subclass must define a constructor such as:To see that it matters, instead of defining the above, add a default constructor to the super class such as:
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Tom : Did you read my post? The code as written runs fine,
if 'msg' is static, because Apple is not instantiated.

Jim ... ...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Jim Hoglund wrote:Tom : Did you read my post? The code as written runs fine,
if 'msg' is static, because Apple is not instantiated.

It should not run fine, Tom is right, the code that you gave in your last post will not compile as Apple must call the super constructor explicitly by passing a String as argument...

[How did I miss this problem in my previous post ]
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
For me, in NetBeans 6.9, the code shows the error but also runs.
Here is the code and its output. Notice that the variable 'msg' must
be declared static, however. And again from my previous post...

After puzzling a while I think this is the cause. Since Apple has no String constructor
(just the default), and Fruit has no default constructor (only String), there is no way
to initialize an Apple object. Clever compiler - ey?

The code runs because it does not try to create an Apple object.

Jim ... ...

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Jim Hoglund wrote:The code runs because it does not try to create an Apple object.

It doesn't matter. The code should not compile. It doesn't compile in the javac compiler that comes with the JDK and that's what the SCJP exam is based on. IDEs can behave differently at times from the default behavior of the compiler. Try to compile your programs from the command line using the JDK compiler for SCJP preparation...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Jim Hoglund wrote:
The code runs because it does not try to create an Apple object.


Java syntax is wrong. Then how can it run?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Milind Chaudhary
Greenhorn

Joined: Jul 09, 2010
Posts: 20
Because the super class Fruit does not have a default constructor (one with no parameters), the subclass must define a constructor such as:



Is that a rule? because i have not read it.. till now.. maybe it would be a few pages later..
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Milind Chaudhary wrote:
Is that a rule? because i have not read it.. till now.. maybe it would be a few pages later..

Yea. Because the default Apple constructor calls the super() in it's implementation. But there is no no-args constructor in the super class(Fruit), we should implements our(Apple) constructor to make the object creation chain
Milind Chaudhary
Greenhorn

Joined: Jul 09, 2010
Posts: 20
Thanks for your answer..

That is why I like this forum so much.. because one gets answers within minutes.. the biggest and most active forum i have seen till now..

Just another question-
Can anyone suggest a really good book for algorithms and data structures?

I know that this is Javaranch and meant only for Java but i see brilliant programmers here so wanted a little tip from all of you.. You have got to help me.. I want to study these topics for getting a good internship..
Expecting a positive response..
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Milind Chaudhary wrote:Thanks for your answer..

That is why I like this forum so much.. because one gets answers within minutes.. the biggest and most active forum i have seen till now..

Me too.

Milind Chaudhary wrote:
Just another question-
Can anyone suggest a really good book for algorithms and data structures?

I know that this is Javaranch and meant only for Java but i see brilliant programmers here so wanted a little tip from all of you.. You have got to help me.. I want to study these topics for getting a good internship..
Expecting a positive response..


Most of the time, here, one topic for one thread. Start a new thread with your topic. I think, for this(your second question), Java General is a appropriate forum
Milind Chaudhary
Greenhorn

Joined: Jul 09, 2010
Posts: 20
Sure .. as you say..
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Caution : There must be a particular constructor signature only if it is
called. Below, although 'A' has no default constructor and 'B' has no
String constructor, this is clean code. Jim ... ...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Jim Hoglund wrote:Caution : There must be a particular constructor signature only if it is
called.


No idea ! Does A and B have IS-A relationship?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
In this example 'A' and 'B' are Has-A for each-other. But not needing
an unused constructor is the same as in Milind's original Is-A example
(with a static msg).

As for Milind's code not compiling, can anyone explain why this should
be so? It makes sense to me that the code should run okay. What does
not make sense is the compiler error.

Jim ... ...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Jim I can't understand your point with the HAS-A code that you posted. There is no need to write a constructor in B class, if you don't create any, a default no-arg constructor is automatically created for you by the compiler. HAS-A relationship is completely different from IS-A relationship. If we watch a simplified version of Milind's code, it looks like this
Here since we didn't provide a constructor to Apple class, one will be created by the compiler (even though it is never called, still it will be created as it might be called from another class in a different .java file). So the above is converted to this by the compiler
Since an automatic call to super is added as the first statement of Apple constructor by the compiler, so you get an error. This is why you either need a no-arg constructor in Fruit class over explicitly define a constructor in Apple class and call super constructor with a String...
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Ankit : Please focus on my second paragraph. It's the only open question; the "bogus"
compiler error. Do you have something to offer on that question?
Jim Hoglund wrote:As for Milind's code not compiling, can anyone explain why this should
be so? It makes sense to me that the code should run okay. What does
not make sense is the compiler error.

Jim ... ...

(P.S. I understand is-a, has-a and default constructors very well.)
Pradeep Kr
Greenhorn

Joined: Feb 17, 2010
Posts: 22
Jim, I agree if we were able to compile code, there would be no problem during run time.


But in order to run, we need a compiled code. and the problem with this code (in simplified code) is already mentioned by Ankit Garg



Above code is not compilable and hence there is no question of run-time.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Many posts ago I started by saying that in spite of the compiler error,
this code does run in NetBeans. I have not investigated further in
NetBeans, but studying the code, I have concluded that it should run;
that the compiler error is incorrect.

So the remaining question is about the compiler. It is not valid to
assume that the compiler is always correct since it is written by folks
just like us (well almost like us), Can anyone explain why the code
should not run?

Jim ... ...
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
without .class file how could any IDE can run ......
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Jim wrote:Ankit : Please focus on my second paragraph. It's the only open question; the "bogus" compiler error.

"bogus" compile time error?? believe me its a very logical compile time error. I don't know how that code runs in Netbeans (maybe there is already an Apple.class file which was compiled when Apple.java had legal code), but for the purpose of SCJP we have to focus on the javac compiler that comes with the JDK and in that the code doesn't compile. Lets see the code again
This code would generate Fruit.class and Apple.class (if it compiled successfully as you are saying). You are saying there should be no problem as I've not instantiated Apple class. But what I've not done in this code doesn't mean I can't do it anywhere else. Now I create a Main.java file which has the following code
I put the Main.java file in the same folder as the other classes. Now this code should also compile fine. Now what will happen at runtime?? When Apple class' constructor runs, it will have to call the super class constructor (I hope you know that the first statement of the constructor of a class is call to super constructor, if we don't put such a call ourselves, the compiler adds it automatically). But the Apple constructor cannot call Fruit constructor as Fruit constructor requires a String. I would suggest you to take another look at my last post again. The Apple class will be modified by the compiler to add a constructor to it with a call to super. It is because of that constructor that the code fails to compile. Every class has a constructor whether we create one or not. And constructors are bound to call super class constructor as their first statement*, if we don't call it explicitly, its called implicitly i.e. the compiler calls it.

* A constructor cal call either the super constructor or another constructor of the same class as the first statement i.e. either super() or this()
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Ankit : More research in progress . . .

Jim ... ...

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Okay, I've found that the code does indeed run in NetBeans 6.9, hand typed into
a new project. So there can be no "different versions" hiding in the background.
But I confess that at this point I don't really care why.

Some other thoughts: The compiler certainly adds the default constructor for 'Apple'
and then, having done this, cannot find a corresponding no-arg constructor in Fruit.
The result is the same if constructor "Apple() {};" is hand entered. Hence the error
message. But let's take it further.

Adding constructor "Apple(String str) {};" does not solve the problem either, because
the compiler inserts a call to super() as this constructors first statement. Finally, the
version below eliminates the compiler error.Jim ... ...

Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

In my opinion, this is a logical compiler error and should never run. Why? Well, the compiler inserts a default no-arg constructor which content is " super();", unless another constructor is specified, which in this code, doesn't happen. That means after the compiler inserts this code, there is clearly a constructor called that doesn't exist, which always does, or always should, result in a compiler error. NetBeans, however, apparently allows you to compile such code.

If you insert:


in a random class with a main method, ignore NetBeans' warnings that it 'cannot find symbol' and run anyway, it will compile and run, though it does have a compiler error in it. And when you should actually happen to call that method, it will throw a RuntimeException that says "uncompilable source code". So your code never got compiled. NetBeans just compiled what it could compile and left everything else out, and only when you call that code that couldn't compile, only then does your program explode.

Now isn't that funny.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Hmmm......

Can we call the implicit constructor given by the compiler No-Arg-Just-Super()-In-Body-By-Default constructor?

Anyone with better suggestion can pitch in.


Regards,
Avishkar Nikale
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

It's as simple as this: NetBeans will run anything. She wil let you know it's a compiler error, and she will warn you with an alert popup before you run your program, but if you ignore everything, she will still run, and try to compile and run everything she can, and when she can't go any further, she will throw a RuntimeException letting you know that she didn't go on because she didn't want to and she thinks it's up to you to do your job and write code without mistakes.

Conclusion: it IS a compiler error. NetBeans tries to run it as best as she can, but only because you're forcing her. You rascal you.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

I didn't know NetBeans was a woman


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Can we call the implicit constructor given by the compiler No-Arg-Just-Super()-In-Body-By-Default constructor?

That is the constructor we call when we instantiate the class i.e. like new Apple();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor not found error