Help coderanch get a
new server
by contributing to the fundraiser

Yogesh Gnanapraksam

Ranch Hand
+ Follow
since Dec 17, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Yogesh Gnanapraksam

yes..Thanks Mike.
11 years ago
Thanks Mike,What happens if each of the elements in elements array in Stack class refers to a linked list, assume a case where it a stack of lists , in that case we will get into the same problem right ?
11 years ago
Question on clone :

Hi,
I am reading JOsh Bloch's effective java (2nd edition) , I have a question in

the topic 'Override clone judiciously' (Item 11) page no: 57

It provides an example of a java class providing a stack functionality. The

author cautions that merely calling super.clone() will result in the cloned

Stack's array holding the elements referring to the same array as the Original

Stack instance. Modifying the original will destroy the invariants in the clone

and vice versa.



He suggests that to avoid this situation we have to call clone on the array

recursively.

I am able to understand till this point.
Now he cites another example
public class HashTable implements Cloneable {
private Entry[] buckets = ...;
private static class Entry {
final Object key;
Object value;
Entry next;
Entry(Object key, Object value, Entry next) {
this.key = key;
this.value = value;
this.next = next;
}
}

He cautions that the strategy that is used in cloning the Stack class does not work here..
/ Broken - results in shared internal state!


"Though the clone has its own bucket array, this array references the same linked lists as the original"

I am not able to understand why the same problem is not applicable for the Stack class .

Please help me understand this concept ..
11 years ago
There seems to be no issues with the line of code you have provided here. It should work fine.
12 years ago
Your class implements Runnable but I don't see any code that starts a thread .
Look at this tutorial
12 years ago
There is a general design principle that you have to program to an interface (or supertype) ,not an implementation.
In you code you have tightly coupled your database instance variable to a concrete implementation ,as you have been told there could be change in the database which would require a modification to your code.
It is better if you have your class open for extension but closed for modification. Google for the 'open/closed principle' and you will find many articles about this.
12 years ago

this is a reference to the current object



More details here

Just try printing 'this' to the console..
12 years ago
You are stumped because you are new to Object Oriented Programming.
You have written a class and to make use of the members of the class you have to create objects. Only then you can invoke the non-static members of the class.
This might be useful.

.


12 years ago
Hi Winston,
I am trying to get a better understanding with help from experts.I will put them into practice/educate my co-workers with these findings.
Regards
Yogesh
12 years ago

So.... for this example, the "suggestion" argument probably doesn't apply here.



Yes. I was trying to get to this point. Thank you.
12 years ago
System.gc() just makes a suggestion to run the GC and it is upto the JVM to decide to run it or not. From the above code runs ,it seems that JVM runs the GC (provided the object is not reachable) when System.gc() is invoked. It does not exit without running the GC. In this case too JVM might have exited as well without running the GC but it complied with the request.
12 years ago

Why is this surprising???



Why is this not happening in Test 2 ?
12 years ago
I became a little curious and tested this out in the following ways..

Test 1:


Output : hi


Test 2 :



Output : hi

Test 3:



output :

hi
garbage collected


This is where I am surprised. Call to Sytem.gc() makes the JVM run finalize!!

I commented only the line 'obj1 = null " only and then tried again..

Test 4 :


output : hi

Now finalize is not invoked.
12 years ago
You have no control over running the GC in java. It is completely decided by the JVM. By invoking System.gc you are simply requesting the JVM to run garbage collector which will effectively be JVM's decision.
12 years ago

int a[0] = 96;



This is not a valid expression.
12 years ago