Yeming Hu

Ranch Hand
+ Follow
since May 14, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Yeming Hu

what is eligible for GC is object not reference variables. c3 has never point to an object. There are two objects of CardBoard type, each of which have a reference to a Short object. Therefore, There are 4 objects. c2 is not assigned to null although cb is because cb only have c2's value when c1.go(c2) is called. cb and c2 are defined in different methods. At last, there are only two objects are eligible for GC, one object of type CardBorad referenced by c1 originally (now c1 is null) and the other object of type Short referenced by the former object. These two objects are isolated island now

Originally posted by Marc Wentink:
Actually this is confusing me even more:



DOG && CAT are constant so when they're accessed, compiler will use the class name (here enum name) to replace the the reference variable. Therefore an object instance is not needed. In your example, the compiler will replace s_a.DOG to Animals.DOG. It's similar to the static variable of a class.
[ August 08, 2007: Message edited by: Yeming Hu ]
Quoted from Java API spe:
boolean add(E e)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

boolean offer(E e)

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E), which can fail to insert an element only by throwing an exception.

Refer to Queue API
[ August 08, 2007: Message edited by: Yeming Hu ]

Originally posted by Vivian Josh:
No, I fell for the same trap

I checked all 4 answers because it evaluated to 1111. But only ans true is the given one and in that case x becomes 100 . (Initial x =0 and all other cases were in if() statement.)



Not really, It's not the trap. please give the options. I can explain them
My understanding is that, in Line A, "Hel" and "lo" are string literal(or string constant), so "Hel"+"lo" is the same as you write "Hello". However, in Line B, "Hel" is still a string literal(or string constant) but lo is a string reference and its value is not determined at compile time so the value of ("Hel" + lo) is not determined at compile time but run time. Therefore, a new string is created for "Hel" + lo.
It means all the conditions are true: 1000+100+10+1=1111.

Originally posted by dolly shah:
Yeming, Thanks for explanation. But Is this not a method call before supertype constructor has been called?
Animal(){
this(getFood());
System.out.println("No-arg constructor completes");
}


Can you explain little more? I am not getting exactly.



Yes, getFood is called before the super class constructor completes but it's static. If it's instance method, there will be compiler error. Here I just want to show that Constructor is invoked, and then instance variable and then constructor completes which you asked.
You can the search keyword "MapEQ" using the search function on the top right of pages and find a couple of topics about this question.
Here is the Link

Originally posted by sumi selva:
Hi,

I tried the following code
class Test {
static int x ; ---> line 1
x = 3;
public static void main(String[] args) {

}
}

even if I substitute int x (or) final int x (or) final static int x
I get a compile error.

Should a instance variable always be a compile time constant.cant we assign them values at runtime.

Thanks,
Sumi



x=3 should be in a method or initialization block.
Although 1.4 and 1.5 both treat assert as a keyword, assertion is disabled for 1.4 by default while enabled for 1.5 by default when compiling. I believe K&B talks about it when it introduces assert so you can refer to it.

Originally posted by Raghavan Muthu:




When it comes to the initialiazing of superclass through constructor chaining, the instance variable "i" of superclass has to get the value by calling the method "getFeet()" (as per the code). Obviously, you can call ONLY the static method(s) during or within the constructor because to invoke an instance method, there is "NO" object existing and thats what you are doing it now.



Not necessarily. Before the super class constructor completes, you can't call the instance members. But You can call the instance members during or within its own constructor because the super class constructor already finishes at that time. For my example, i can be assigned a value by a instance method.
We can refer to Coery's Object Initialization sequence and JLS

Originally posted by dolly shah:
Thanks both of you.
But I am confuse about Constructor chaining which is written in K & B book, chapter-2, page-128. It shows, constructor invoked then instance variable then constructor completes. I am not getting this chaining.
Can you explain please?



I am not sure whether I get what you mean, I hope the following code can help you:

class Animal{
int i = getFeet();
String food;
static int getFeet(){
System.out.println("instance variable initialization");
return 1;
}

static String getFood(){
System.out.println("Constructor invoked");
return "Food";
}

Animal(){
this(getFood());
System.out.println("No-arg constructor completes");
}

Animal(String food){
this.food = food;
System.out.println("Arg Constructor completes");
}

public static void main(String[] args){
new Animal();
}
}

Output:
Constructor invoked
instance variable initialization
Arg Constructor completes
No-arg constructor completes
[ July 31, 2007: Message edited by: Yeming Hu ]
The following code is my understanding:

class Animal{
}

class Horse extends Animal{
int i;
Horse(int i){
this.i=i;
}

Horse(){
this(get());
}

int get(){
return 1;
}
}

There is a compile error because the get() is called before the super class constructor is completed.
[ July 31, 2007: Message edited by: Yeming Hu ]