Help coderanch get a
new server
by contributing to the fundraiser

Ravi Ahuja

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

Recent posts by Ravi Ahuja

post the question
Everyone wont have the book which you are referring to.
Hi ketki
now in the following code
lets see wat happens
the ball starts rolling in the main method
where amethod is called of ObParm class
now in amethod
we are creating an object of class Valhold
and setting its member variable to 30
Hope its okie for you till now
Now here is were you may be lost
now the next line

another(v,i);


in this method we are passing the reference created in amethod
Now in java when you pass a reference variable into a method you are actually sending the path of the reference variable and you can access and change the value of the member variable of
class and it would be reflected in the method.
whereas if you change the reference variable itself and you are making any change it wont be reflected it is as you are creating a new reference variable
well lets take it in this way

v.i = 20;

here you are accessing the variable i of ValHold class this change would be reflected above in the amethod
the value of i would be set to 20 from 30


now here

v = vh;

you lost the address of the object
it is not accessible now in this method
hence the value of the variable i stays 20
for both version you have to be practical
Hello!
The given answer is correct it becomes eligible at line 13.
According to GC the object are eligible for garbage collection when it is not accessible by live thread.

8. sb = new StringBuffer("Manchester");


Now here an object is created in the heap

sb --->> "Manchester"

sb is holding a reference of manchester.
Manchester is an object in the heap not sb .


Now
9. StringBuffer sb2 = sb;
after this line


sb--->> "Manchester"
sb2-->> "Manchester"

---------------
10. StringBuffer sb3 = new StringBuffer("Chester");

now after this line
sb-->> "Manchester"
sb2-->> "Manchester"
sb3-->> "Chester"
-----------------

11. sb=sb3;
Now after this line



sb-->> "Chester"
sb2-->>"Manchester"
sb3-->>"Chester"

after line 12
that is
12. sb3=null;
---------
sb-->> "Chester"
sb2-->>"Manchester"
sb3-->> NULL

after line 13
this is what happens

13. sb2=null;
sb-->> "Chester"
sb2-->> NULL

sb3-->> NULL



so the reference variable the only reference variable holding the reference of manchester is set to null and hence the object created at line 8 (Manchester) is eligible for garbage collection


Hopefully you got it ..!!

And they have asked the Object eligible for GC not the reference variable ..this is were you are losing it
One advice would be if you flunk in the mock test very badly say you are
scoring 40 -60% dont review the answer instead read the book once again and try the same mock exam and may be after that you can get the idea about your score .
The way you are going checking your answer is wrong i guess because the next time you are giving the exam you know what is the answer of the question.
So try the above thing.
Hello ranchers
Today I have cleared scjp with 96%
a big relief after a long long time thanks to all fellow ranchers who have helped solve my doubts.
I lost marks in garbage collection and operators and assignment.
As of now in a bit hurry will share my experience after some time.
Thanks to Everyone once again!
17 years ago
Thanks chandra
So a subclass can use a dot operator to access a protected member of the superclass
fine thanks a lot relief now

Sorry Barry
will follow it next time
Thanks Chandra
import package foo.*;
sorry for this one
it should be import foo.*;
actually i was in a hurry sorry
Waiting for your quick response thanks everyone once again
Hello Ranchers
I am taking the exam tommorow and i have one query
My pc isnt working and so i cannot compile and see the behaviour of the following java file
Please Help me out right now i have logged in from cyber cafe
Without wasting the time i am posting the question
[code]

//Test.java

package foo;
class Test
{
protected void methodA()
{
}
}
------------
//XYZ.java
package bar;
import package foo.*;
class XYZ extends Test
{
public static void main(String ar[]){
XYZ obj=new XYZ();
obj.methodA();
}
}
what will be the output .
I guess The output would be a compile error
but its using the dot reference on the subclass so i am a bit messed up please clear guys
Thanks
[code]


One more Question
//Test.java
package foo;
class Test
{
char methodA()
{
}
}

======
//XYZ.java
package bar;
import foo.*;
class XYZ extends Test
{
public static void main(String ar[])
{
Test t=new Test();
t.methodA();
}
}
What is the output i guess again compile error because the methodA is having the default please just clarify thanks a lot
[code]

Edited title:like Bert wrote below (it may be urgent to you, but it is not to the majority)
[ June 03, 2007: Message edited by: Barry Gaunt ]
I agree with what Raghavan said lots of free questions are available online how could we know the thing is a braindump ?
Anyways dont want to debate
I am Sorry If i have done any mistake while asking any question
Anyways wont happen again ..
Sorry Barry and fellow ranchers
[code]
class animal
{
public Object go()
{

return new Object();
}
}

class dog extends animal
{
public car go()
{

return new car();
}
}
[code]
----------------------------------------------------------------
You are not overriding the method properly
Just go through the rules of overriding
int tricky =0;
for(int i =0;i<3;i++)
{
tricky += tricky++;
}
System.out.println("its"+tricky);
-----------------------------
The postfix operator works after we use the variable
in the above case
when it enters in the loop for the first time
value of tricky=0
now
doing this
tricky+=tricky++;
the value of tricky would be 0//1
than i is incremented
and it enters the loop for the second time
now remember the value of tricky=0 since we have assigned the value to it see 1
than again this line gets executed
tricky+=tricky++;
the value of tricky again is assigned to zero since the postfix operator works after we use the variable
similarly for the third time it enters with the same value of tricky ie 0
-------------------------
You can try out this code to know the difference between postfix and prefix

int trickypost =0;
int trickypre=0;
for(int i =0;i<3;i++)
{
trickypre += ++trickypre;
trickypost += trickypost++;

}
System.out.println("its"+trickypre);
System.out.println("its"+trickypost);