Bob Lulyk

Greenhorn
+ Follow
since Nov 05, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Bob Lulyk

Dear Amit:
The Java Language Specification basically says that a Java
source file consists of the following 3 parts:
1. An OPTIONAL package statement
2. ZERO or more import statements
3. ZERO or more class definitions
Therefore, there's nothing stopping you from compiling
successfully an empty source file (of course, it's absolutely
useless -- but that's another matter entirely).
Hope that helps.
Dear Nasir:
Instance variable a (defined at top of class Qcb90)is visible
inside the 'g' function, so when the function executes 'a = 1' the variable's value changes to 1 before being printed out.
Hope that helps.
Dear Singh:
String name; // JUST DEFINES STRING REFERENCE
String newName = "Nick"; // DEFINES STRING REFERENCE AND
// POINTS IT TO "Nick" OBJECT
newName = "Jason"; // REPLACES REFERENCE TO "Nick"
// OBJECT WITH REFERENCE TO
// "Jason" OBJECT, MAKING "Jason"
// ELIGIBLE FOR GARBAGE COLLECTION
name = "Frieda"; // POINTS STRING REFERENCE TO
// "Frieda" OBJECT
String newestName = name; // DEFINES STRING REFERENCE AND
// POINTS IT TO "Frieda" OBJECT
// ALSO
name = null; // REMOVE 1 OF 2 REFERENCES TO
// "Frieda" OBJECT
Therefore, only 1 object is eligible for garbage collection
at point indicated by "//Line A" and answer b) is correct.
Dear Ajay:
I believe what the authors mean by "the caller of the method abandons the popped value" is that the program which called the
method has no further use for the object reference which the
method returned. However, unless either:
(1) that object reference is replaced by a null reference as in the revised code sample; or:
(2) that object reference is replaced by a reference to another object (presumably via a "push" method or its equivalent)
the garbage collection system has no way of knowing that it is
safe to reuse the memory holding the object being pointed to by
the "popped" object reference.
Hence their statement "until the array element containing a
reference to it is overwritten".
Hope this helps.

Alternately, the following illustrates the concept as stated on
page 99 of the Java Language Specification ("if the member or
constructor is declared private, then access is only permitted
when it occurs from within the class in which it is declared"):
*****************************************************************
class OtherClass
{
private int otherClassInt_ = 1;
}

class SameClass
{
private int sameClassInt_;

SameClass(int sameClassInt)
{
sameClassInt_ = sameClassInt;
}
void showSameClassInt(SameClass sameClassInstance)
{
System.out.println(sameClassInstance.sameClassInt_);
}
//void showOtherClassInt(OtherClass otherClassInstance)
//{
// System.out.println(OtherClass.otherClassInt_);
//}
}

class test
{
public static void main(String[] args)
{
SameClass instance1 = new SameClass(1);
SameClass instance2 = new SameClass(2);
instance2.showSameClassInt(instance1); // DISPLAYS "1"
instance1.showSameClassInt(instance2); // DISPLAYS "2"
}
}
This example compiles and executes properly only if method
"showOtherClassInt" is commented out; if it is not commented out, then you receive the error message "otherClassInt_ has private access in OtherClass" upon compilation.