Hi,

I was preparing for SCJP 1.6 and i have the following snippet of code.

package com.self.trials3;

public class Animal {
	public void Animal(){
		System.out.println("Obviously Works");
	}
	Animal(){
		//System.out.println("Constructor");
		//super();use super or this() as first line in your cons to call other //cons.
		this("Adarsh");
	}
	Animal(String AnimalName){
		//System.out.println("AnimalName >> "+ AnimalName);
		this(); //cannot use , recursive cursor invocation infinite loop
	}
}

When the above code is used in my eclipse , i get the error that is recursive cursor invocation infinite loop. But the SCJP book says that this should work fine , but in turn gives a stack overflow error during runtime which is obvious should it compile.

My question is whether this has been a new feature added to java 6 or am i missing something ?

Recommended Answers

All 7 Replies

Obviously , it goes into infinite loop and that's why the compiler reports an error message.

But the SCJP book says that this should work fine

I assume you are talking about SCJP by kathy sierra.
Can you say at which page of the book , they said that it will work fine.

> My question is whether this has been a new feature added to java 6 or am i missing something ?

This compiler enhancement was added in Java 5. The above code compiles fine in Java 1.4. I'd recommend getting the most recent version of the SCJP book if you plan on giving SCJP 6 to avoid such inconsistencies.

@ above.

Well i do have the latest edition of their book. Purchased a copy. They said the compiler does not catch the error.

@ harinath_2007

You can check pg 143 and 144.

Well i do have the latest edition of their book. Purchased a copy. They said the compiler does not catch the error.

What is the name of the book? SCJP Sun Certified Programmer for Java 6 Exam 310-065? If yes, are you "very sure" it specifically says "does not catch"? Can you paste the exact wordings the text uses?

Yes that is the book.
Check page 143 and 144 for the same. I have pasted it below for you to cross verify.

<snip>
What do you think will happen if you try to compile the
following code?

class A {
A() {
this("foo");
}
A(String s) {
this();
}
}

Your compiler may not actually catch the problem (it varies depending on your compiler, but most won't catch the problem).

I'm not sure if i'm supposed to copy paste material like this. The last time i did i was warned. Hope the people see this as a genuine issue.

Thanks

I've edited out your post so that it only contains the relevant part to comply with the site rules.

Anyways, the important portion in the text above is "may not catch". It doesn't say "won't catch" or "would not catch". It's just that the textbook assumes that a lot of compilers out there won't catch this issue but it's a fact that Sun JDK 1.5+ does catch this issue. I don't have a *nix box handy right now but I'll check with some other compilers (jrockit, gcj etc.) and see if they issue the same error.

Recursive constructor invocation is always a compile time error..!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.