P Hunjan

+ Follow
since May 20, 2005
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by P Hunjan


This is a question from Dan chisholm question set.
The answer is Compile time error which I could not understand.

Why can't an exception be caught if its not thrown for sure ?
I was guessing that it may be because its a checked exception & we have'nt used 'throws' clause to specify that it might be thrown.

Hi Smitha,
As Marc said any instance of that class will be a thread & will have an "implicit" reference 'this' to all instance members.
Its not mandatory in such a situation to use 'this' you can call directly as well. 'this' is generally used to differentiate between a local variable & the field which it hides or shadows.Therefore,this.start(); & start() will have the same meaning in this context.

Hope it helps !
Hi Smitha,

I guess the second method's parameter java.lang.Character is an object(Wrapper Class), therefore when you pass 'b' it converts it into 'int' & calls the first method.

Thanks a lot for your reply.

I was guessing that when we create an anonymous class in static context. The anonymous class also implicitly gets the status of being static and in this situation the static reference can refer to this anonymous class.

But I tried writing code, where an anonymous class in non-static context is refered by a static reference & obviously it didnt work.

So I am still confused ?

And theres one more question related to this.
Local & anonymous classes cannot be marked as static but they can be created in static context & then they are implicitly static.

So why is that constraint ?

Would be great if someone can throw some light on this


The following question's correct answers are b & d.
I am not able to understand how option d. is correct.

Can someone explain please !


Which of the following are true statements?

a. An anonymous class is implicitly abstract.
b. An anonymous class is implicitly final.
c. An anonymous class is implicitly static.
d. A static reference variable can reference an instance of an anonymous class.
e. An anonymous class declaration must have at least one explicit constructor declaration.
f. An anonymous class declaration can have more than one explicit constructor declaration.

This operator confuses a lot
You can try reading;http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#80289

Also Marcus Green says "Note that instanceof tests against a class name and not against an object reference for a class."

I guess it checks at the compile time if the two references are not convertibles to prevent it from reaching runtime & raising an exception.

Would be great if someone else can write something on this.


Theres a question by danchisholm;

class A extends Thread {
private Object obj;
public A(Object obj) {this.obj = obj;}
public void run() {
try {
synchronized (obj) {obj.wait();}
} catch (InterruptedException ie) {}
class B {
private void m1() {
for (int i = 0; i < 10; i++) {
A t1 = new A(this);
t1.setName(String.valueOf(i)); t1.setDaemon(true); t1.start();
synchronized (this) {notifyAll();}
public static void main(String[] args) {new B().m1();}

What are the possible results of attempting to compile and run the program?

a. All of the numbers 0 through 9 must always be printed
b. Some or all of the numbers 0 through 9 could be printed
c. Nothing is printed
d. Run-time error

The answer is b&c.
There are no chances that any number will be printed because daemon thread will die as soon as the main thread finishes.
Is it correct to give a. as an option too?

Thanks in advance.
Negative zero compares equal to regular zero (positive zero), but the two zeros may be distinguished by division: one divided by negative zero yields negative infinity; one divided by positive zero yields positive infinity.
Thanks for your detailed explanation.

If i am not wrong this time.
I can say that as when we pass a copy of reference, we cant change the object but can access its variables & methods & changes to the variables persists even out of the methods.

Can someone please further explain it, why it prints 20 instead of 30.
Nested Classes are defined with Static keyword & are considered as Top Level Classes. They don't require the instance of outer class to create their own instance, whereas an inner class is tied to the instance of outer class.


yes,I meant the local inner class variable.I got confused by coming across the same statement in different reading materials that "An object can live beyond the scope of the bloc of code inside which they are defined". So I tried to do that but in a wrong way.

It clarifies, scope dies but not the object & we can use it in different way. It will be really great if i can get an answer to my another query i have stated regarding different outputs.

P Hunjan
I guess i got confused with an Object's life & a variable's scope.
I modified my code which I was trying but I am not getting the desired answer, rather theres something new i found->

Initially I tried this;

Output is 0 rather than 50

When I modify it to->

This time output is 50 for both the variables.

( tags added and formatted)
[ July 18, 2005: Message edited by: Barry Gaunt ]
"Local inner classes :
This type of inner class is defined inside a bloc of code. However, like any object they can live beyond the scope of the bloc of code inside which they are defined, this depends on what you do with them."
I am trying to write code where i can use a Local inner class's variable outside the scope of its block of code. But no luck
Can anyone send me an example?