Anit Nair

Greenhorn
+ Follow
since May 09, 2008
Merit badge: grant badges
For More
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 Anit Nair

Hi,

Thats true only if the two instances are created directly by assigning values directly i.e.


However,when you create two instance using the constructor, two different objects are created
In the below statement using wild card ? and super just means the type of collection you can assign to list should String or Parent type of String ex. Object. However if once you assign a collection of type <A> only objects you can add to the list are it are objects A or its sub-types. You cannot added objects that are above A in inheritance hierarchy.
At compile time there is no way for compiler to know what object the reference variable points to, in this case it may be object of A or object of B. Since you are using reference of class A and the method defined for class a throws exception, compiler will generate error if the exception is not handled or declared.
Dear friend,

Whenever you pass a generic collection type to a non generic collection type, as you are doing in the ex. mentioned, there are two scenarios to be taken into consideration.
1)You are just reading the values
2)Are you modifying the collection within the function

1) In this case the code will run without warning
2) In this case regardless of the type of Object you are adding, it will compile with warning. The reason being since you are passing a generic collection type to a non-generic type (ie pre-Java 5) the Java compiler warns that object you are adding may cause problems. This is because there is no way to detect what the type of object is at runtime. Because all the generic information is compile time information.



Hope this helps.
I have cleared SCJP with 100% score. I am planning to give SCWCD, please suggest me the best books and materials for the same

And
Thanks to all ranchers for all the support.
[ July 26, 2008: Message edited by: Anit Nair ]
15 years ago
yes i did, try out a similar example.I think regardless of the generic type passing a List reference to Arraylist reference would give compilation errors. Let me know if you any other thoughts on this.
Following is the piece of code from K&B 1.5 pg 587

Besideds above code theres a parent class Animal and Dog class extends Animal.
Now compiling above code gives error

The explanation given for error states that ArrayList of subtype cannot be assigned to ArrayList of supertype, rightly so. But I feel that even if the
checkAll method was modified as below

It would still give compilation error just because we cannot pass ArrayList reference to a List reference regardless of the generic type used. Please correct me if i am wrong.
I have a query regarding Ch10 self test Q10. It states there are three version of MyClass.java
in three diff folder namely

As per what i understand javac looks for .java file in the current directory by default unless the file path is specified. In above case there is no MyClass file in /foo directory so java compiler should not be able to find the file. But the answer says that version MyClass.java in
/foo/bar/baz/bing will be compiled.
Method local inner class cannot use method local variables. But it can use method local variables marked as 'final'. Why is it so?
Hi Sridhar, I guess i have understood....whats happening in this case. Actually, the value is never getting stored at all, i guess. Its just that when you de-serialize and cast the object to (DemoSerial), when you execute the following line


Its similar to calling DemoSerial.i hence the value 7. It just prints the current value of i. not the value belonging to any instance of DemoSerial. Thanks for your inputs
Hi Sridhar, thats exactly what i want to say....static member are not part of objects state....then why is the value for it stored in the first place...
If you refer the K&B pg 456 under topic [Serialization not for statics]
The last line clearly states "Static variables are NEVER SAVED as part of object state...because they do not belong to the object". Then why is 7 saved in this case...
As per K&B serialization is not for static variables....but i tried out following piece of code and it shows that static variables are also serialized and saved as part of object state..I m a bit confused now. Please find below the code i executed


Also, if i add static to transient member variables the member variables are still serialized..shouldnt adding transient member variables be skipped
while serialization???
[ May 09, 2008: Message edited by: Anit Nair ]
[ May 09, 2008: Message edited by: Anit Nair ]