Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"

Sidharth Panwar

+ Follow
since Apr 30, 2006
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sidharth Panwar

Go for Kathy & Bert's SCJP book and another good book is Java2 Certification by Khalid Mughal.
Oops! Nilesh already posted the answer.
The output of your program is:

at Z.<init>(
at Y.<init>(
at Z.<init>(
Till a certain no. of times.

Now your program is not running normally and the StackOverflowError has been raised and what you see above is the stack trace done by your JVM. To confirm that the StackOverflowError is raised you can do the following:

public static void main(String s[])
System.out.println("Here it comes");
Z z=new Z();
catch(StackOverflowError e)
System.out.println("StackOverflowError encountered");
the output now prints - StackOverflowError encountered
Damn, why didn't i checked it myself!!! So this new thing does work in a mysterious way. Thanks Arul, now i'm crystal clear on this.
Hi Arul,
Getting the gist of what you are saying. Now see this:

String s = "Hello";
s(Ref. variable, not object) ----> "Hello" (An 'Object' in String Pool)

String s1 = new String("Hello");
s1(Ref. variable) ----> An anonymous String obj. ----> "Hello"

Now if i say,
String s2 = s1;

Will it be:
1) s2(Ref. variable) ----> anonymous String obj. ----> "Hello"
2) s2(Ref. variable) ----> "Hello"

If case 1) is true then it's kinda holding your ear from over your head i.e ridiculous.
If case 2) is true then what happens to the anonymous String object?
In K&B's book on page 420 the text says:

String s = "abc"; //Creates one string object and one ref. var.
In this simple case, "abc" will go to the pool and s will refer to it.

String s = new String("abc"); //Creates two objects, and one ref var.
In this case, because we used the new keyword, Java will create a new String object in normal(non-pool) memory, and s will refer to it. In addition, the literal "abc" will be placed.

Why two objects are created in the second case? Still don't understand why the two cases are working differently. Plus, the second case looks like more inefficient.

In second case if i say:
String s1 = s;
Would s1 refer to the intermediate object pointed to by s or directly to "abc".

Originally posted by Garrett Rowe:

Thanks Gerrett, didn't think about trying it this way.
17 years ago
class MyGeneric<T>
static int i;
---- Other stuff ----

class TestGeneric
public static void main(String[] args)

Gives a compile-time error:
illegal start of expression - System.out.println(MyGeneric<String>.i);

What is the problem?
17 years ago