Hasnain Khan

Ranch Hand
+ Follow
since Dec 15, 2007
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 Hasnain Khan

Hi,



since you have a public class Cat and most probably you defined it in Cat.java, the JVM is trying to invoke the main method in class Cat which is not provided in there. Kindly move your main method in class Cat.

Hope that helped and is clear.
Kind Regards.
Hasnain Javed Khan.
SCJP 5
Hello All,
I passed SCJP 5 yesterday and scored 87%. I would like to start preparing for SCWCD and have a book for it which is " Head First Servlets and JSP ". I have read on several forums that the book is for beginners and would allow to just pass SCWCD. Are there any other good books that would allow to pass SCWCD with a good score? I have used Servlets and JSPs but the my exposure to Servlets and JSPs is not enough for the exam.
Waiting for a favorable reply.
Thanks in Advance.
Kind Regards.
Hasnain Javed Khan.
SCJP 5.0
Hello MuraliH,

The Generic signature List<? super dog> will allow you to achieve polymorphism with generics so you can pass in a List<Dog>, List<Animal11> and List<Object> i.e Dog and anything up the inheritance tree but you can only add Dog or subclass(s) of Dog i.e Anything Down the inheritance tree. If you try to add a super class of Dog i.e Animal11 to the list, it translates to something like the following.

Animal11 a11 = new Animal11();
Dog d = a11 // you can not assign a super class object to a sub class ref.

Hope thats clear and helped
Kind regards.
Hasnain Javed Khan.
SCJP 5.
Hello Marc.
LOL actually I went out for an evening walk and got hit by a car from behind. The dude ran away though
TC.
Hasnain Javed Khan
Hello All,
I passed SCJP by 87% today. I would like to thank all ranchers who helped me with all my posts and specially K&B for writing such a good book to prepare for SCJP. Now I'll be confident in answering other ranchers preparing for SCJP with their doubts and posts.
THANK YOU ALL RANCHERS once again.
Kind Regards.
Hasnain Javed Khan.
Hello kesava,
Thanks for correcting my mistake . I was very sleepy when I was answering the question . My Humblest Apologies for any confusions caused .
Kind regards.
Hasnain Javed Khan.
Hi,



f3 is pointing to a 1 dimension array and f2[2] is pointing to a 2 dimension array i.e {2.6f, 2.7f}

f2[0] is pointing to {42.Of} 1D Array
f2[1] is pointing to {l.7f, 2.3f} 2D Array
f2[2] is pointing to {2.6f, 2.7f} 2D Array

so the if condition will evaluate to false.

Hope that helped.
Kind Regards.
Hasnain Javed Khan.
Hello Vishwa,
Thank you very much for explaination with examples. Could you kindly elaborate more on

Boo is a top-level class.
What does it mean here, to say that Boo is top level class?
It cannot access the members of the class Foo.
See that it behaves just like the classes in case1

(Visualize it just like the code in case1)

What use does this have?
NameSpace resolution



Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
Hello All,
Could someone kindly explain with an example the below paragraph related to Static Nested Classes.

One can define a class as a static member of any top-level class. Classes which are static class members and classes which are package members are both called top-level classes. They differ from inner classes in a sense that a top-level class can make direct use only of its own instance variables.



I'm confused now
Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
Hello All,
This question is taken from JQ+. There were two question using the same code and asking the same question " What will be the output when class Test is run ? " but the only difference was in the main method. One question had the print statement marked as //1 and the other question had the print statement marked as //2 .




The answer to the above 2 questions are as follows

Que 1) It will print 1 ( System.out.println(J.i) in the main method)
Que 2) it wil print j = 3, jj=4 and then 3 ( System.out.println(K.j) in the main method)

And these are my answers which are wrong

Ans to Que 1 ) It will print ii=2, j = 3, jj=4 and then 1 ( System.out.println(J.i) in the main method)

Ans to Que 2) It will print j=3, jj=4, k=5 and then 3 ( System.out.println(K.j) in the main method)

Interface is loaded when a constant is accessed or assigned to a variable.

For Que 1, since i is defined in Interface K and J extends K, it inherits the variable i. So when J.i is accessed, its super interface will be loaded first and initialized and since all the constants are static and final, they will be initialized in the order they appear. The same loading and initialization will happen for Interface J after Interface K is loaded and Initialized.

For Que 2, I applied the same steps described for Que 1.

My answers were based on the above described understanding which obviously is wrong.

Could some one kindly explain it step by step in detail. My exam is on the 22nd and would really appreciate an early response.

Hope the problem described is clear and not confusing. Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
Hello All,
My confusion is that the evaluation of dimension expression in the array access expression i.e [index=2] is evaluated first before the array reference is resolved or does it depend on the dimension expression inside the square brackets e.g array_ref[1] will resolve the reference variable directly and array_ref[index=2] will resolve the array dimension first and then resolve the array reference variable ??
Hope the question is clear and not confusing.
Waiting for a favorable reply. Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
Hello Henry,
Sorry for the mistake I made. My Bad . I chose false and the answer is true. Thanks in advance.
Kind regards.
Hasnain Javed Khan.
Hello All,

The following code sample is taken from JQ+



The questions is if the above class will print '2' when compiled and run.

I chose true but its false

Below is the explanation given by JQ+ but its not clear to me

Explanation:
If the array reference expression produces null instead of a reference to an array, then a NullPointerException is thrown at runtime, but only after all parts of the array reference expression have been evaluated and only if these evaluations completed normally. The embedded assignment of 2 to index occurs before the check for a null pointer.
In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.
Note that, if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.

Could some one kindly clear up/explain it.
Waiting for a favorable reply.Thanks in advance.
Kind Regards.
Hasnain Javed Khan.
Hello All,
I have some confusion about class loading and initialization sequence.Here is what I understood.

1 ) Static statements/static blocks are executed.
2) Instance variables are assigned default values
3) Instance variables are initialized
4) constructor runs
5) Instance initialization block(s) run after all the call(s) to super has(have) been completed but before the rest of the constructor is executed.
6) Rest of the constructor is executed.

This is my sample program.



and this is the output

sM1 called with Parameter a
a
Static Initialization Block
sM1 called with Parameter b
b
sM1 called with Parameter c
c
sM1 called with Parameter 2
2
Initialization Block running
sM1 called with Parameter 3
3
sM1 called with Parameter 4
4
After Instance Initialization Block
constructor called
sM1 called with Parameter 1
1

I was hoping to see 4 printed after 2 but its printing 3 ???

Could anyone kindly explain why .

Waiting for a favorable reply. Thanks in advance.
Kind Regards.
Hasnain Javed Khan
Hello All,
I am confused with the following code. Its taken from JQ+ and had a mistake but the following code is now fixed.

class MyStringComparator implements Comparator {
public int compare(Object o1, Object o2) {
System.out.println("o1 > " + o1);
System.out.println("");
System.out.println("o2 > " + o2);
System.out.println("");

int s1 = ((String) o1).length();
System.out.println("o1 length > " + s1);
System.out.println("");
int s2 = ((String) o2).length(); // it was o1 in JQ+
System.out.println("o2 length > " + s2);
System.out.println("");
int result = s1 - s2;
System.out.println("result > " + result);
System.out.println("");
return result;
}

public static void main(String args[]) {
String[] sa = { "d", "bbb", "aaaa" };
System.out.println("insertion point "
+ Arrays.binarySearch(sa, "cc", new MyStringComparator()) + "\n");
System.out.println("insertion point "
+ Arrays.binarySearch(sa, "c", new MyStringComparator())+ "\n");

}
}

The answer is -2 and 0 . Could some one kindly explain how is it -2 and 0. Since the array is not sorted, the results will be unpredictable -(insertion point) - 1 will be used.The comparator implementation is confusing me. The print statements show the arguments passed and notice that "aaaa" is not passed at all. Waiting for a favorable reply.
Kind Regards.
Hasnain Javed Khan.