Win a copy of Head First Android this week in the Android forum!

Edward Lawrence

Greenhorn
+ Follow
since Apr 02, 2010
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 Edward Lawrence

Thank you Paul. I am relatively new to Java and I am leaning by writing exprimental code. I am closing this question.
9 years ago
Yes. I did get the following exception with the expected results.

Question: Why did it go ahead and produce the correct results after the exception?

java.io.NotSerializableException: Collar
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException
: Collar
Collar@578ceb
5
yes
9 years ago
I think I didn't get an exception because I got the output 5. However, I realized that I did not handle any possible caught exception in my catch block. I probably wrongly deduced that if 5 printed I did not get an exception. I will try it with printing any possible caught exception.

If I get an exception printed my question would be why did it produce the expected result?
9 years ago
The following code is my "java practice" versions of the Dog and Collar classes taken from the book "SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065)" Chapter 6 Certification Objectives—Serialization (Exam Objective 3.3).

I expected the code to produce a runtime exception because the Collar class did not implement Serializable, but the code compiles and runs producing the following output.

Collar@578ceb
5
yes

Question 1. Why didn't I get an exception?
Question 2. What exactly does the output Collar@578ceb represent?






9 years ago
Thanks Fred. That is the fact/concept I have been missing.
I was thinking on the line of your insight until I read the following answer from the book:-

" Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible." The Short is a wrapper object.
If c3 *were* eligible, I believe that nothing would be guaranteed for GC. However, c1 with its associated Short wrapper and c3 with its associated Short wrapper would be eligible. There would be 4 objects eligible for garbage collection. I am probably totally wrong. Can anyone provide a little insight?
Why isn't c3 eligible for garbage collection in the code below? Doesn't it equate to null after c1.go(c2) returns?

The question below was found in the book: SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065), the first self test question of Chapter 3.

QUESTION:- When // doStuff is reached, how many objects are eligible for GC?


The question below was found in the book: SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065). It is the first self test question of Chapter 3.

Why isn't c3 eligible for garbage collection in the following question? Doesn't it equate to null after c1.go(c2) returns?

QUESTION:- When // doStuff is reached, how many objects are eligible for GC?


10 years ago
Why isn't c3 eligible for garbage collection in the following question? Doesn't it equate to null after c1.go(c2) returns?

QUESTION: - When // doStuff is reached, how many objects are eligible for GC?

10 years ago
Just wanted to add this if I remember correctly. When compiling, the compiler thinks sees the element as an Animal and not a Dog. Am I correct?
11 years ago
This is just to very and understand better.
When I created the reference in the array to the Dog object I was implicitly downcasting. Am I correct?

Another clarification / Question
So what is the function of the element I am using on line 48 to create the new Dog object?
Does that element point to a Dog or in reality to an Animal?
In other words I do not quite understand the statement on line 48 although I authored it. Please elaborate a little more for
greenhorn Edward Lawrence.
11 years ago
Given the following 3 classes Animal, Dog, and Array in 3 separate files, I compiled them and ran only the Array class with
the following results:
line 42 output: protected Animal instance variable i = 1
line 46 output: Dog element = pkgb.Dog@45a877

My Questions:
1. Why wouldn't line 48 compile without the cast when line 42 did not need one?
2. why does line 50 produce a compile error without the "//" but line 42 seems to run the
Dog initialization block without errors?


11 years ago
Given the following 2 classes (Dog.java extends Animal.java), please help me with the 2 questions listed below them.



Question 1 - Why do I get the following compile error for line 5 but get no error for line 4 in Dog.java?

C:\SCJP\CODEPR~1>javac pkgb\Dog.java
pkgb\Dog.java:9: <identifier> expected
System.out.println("1-protected Animal instance variable i = " + i);
^
1 error

Question 2 - How do I access the inherited instance variable i in the main method of Dog.java?
11 years ago