Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!

sohail hussain

Ranch Hand
+ Follow
since Sep 06, 2018
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 sohail hussain

Royale Summers wrote:Can anyone explain to me the difference between an Instance variable and a Class/Static variable

every body has explained well,
I want to say that static variables global to all the objects of class, you can update these variable any where from the class and it stores new value every time you update it, but instance variable are not updated for each object. Mean every creation of object cantain its own instance variable
6 months ago

Stephan van Hulst wrote:

Joseph Michael wrote:Can you give me few real time examples where extending Thread has been implemented instead of using Runnable?

There are none. The Thread class should have been final.

sohail hussain wrote:you extends thread class to make your class as thread class, it's ok

It's not okay. Never extend Thread.

In fact, don't even use the Thread class directly. Depend on an ExecutorService instead.

then how can we make concrete class as thread class without extending  please explain i am to confused

Joseph Michael wrote:Hi,

I can create a class extending Thread.class to implement own thread mechanism or i can create a thread class implements Runnable

1) When to create a class by extending Thread.class? In which situation or use case we need to go for it
2) Many examples extends Thread.class and many uses Runnable. Not sure what is the best pratice


suppose you have a class, and you extends thread class to make your class as thread class, it's ok but if you want to extends one more class to the same thread class so not possible because multiple inheritance not possible example
Class A extends Thread{} // ok thread class A
Class A extends Thread, B{} // not ok
Class B{}
But where as implementing runnable interface OK example...
Class A extends B  implements Runnable{}
7 months ago

Campbell Ritchie wrote:

sohail hussain wrote:. . . when you Creat a string object with "hello" and if you try Creat another string object with the same content then compiler first look into the string literal pool . . .

That is not correct, I am afraid. automatic interning of Strings only happens when the Strings are compile‑time constants.

but string is implicitly final  and extends directly the Object class so where is the concept of compile time constant
How ever compile time constant mean variables must be final so they should not change there values at runtime
7 months ago

Mike Gualeni wrote:hello

You're right. But this behavior I didn't read in no book!! But I think it's an important behavior.

If I change the values in the code vice versa final int x = 2; and final int y = 0; the output is:


because no break stops from falling down! And now the program choose case x: {System.out.println("A");}!! Strange behavior!!??

let me help you
In switch statements first of all, it looks for a perfect match by directly jumping into the matching object are values,, because switch assumes that all the cases are same in nature that's the reason it creates a jump statement and after it finds the correct value then fall into the immediate case and continue with rest of the cases until you provided the break statement to break the fall of switch

Campbell Ritchie wrote:

Adrian Grabowski wrote:. . . if you create bunch of strings with the same value, the values will be stored in the intern pool... . . . .

Please explain more; I am not sure that is quite accurate.

when you Creat a string object with "hello" and if you try Creat another string object with the same content then compiler first look into the string literal pool that the object you created is already there in pool are not that is called interned object, if it finds the same content of your object it will not create new object instead it assign the new reference to the same object that mean one object two references
7 months ago

Donald Silverburgh wrote:Hi all,

I am a newbie and I have started learning Java from O reilly. Can anyone guide me any easiest book which can help me learn JAVA from Baby Steps.


you can go with head first java book or you take Herbert schildt book
7 months ago

Mike Gualeni wrote:I have the following code:

Why the switch expression take case y? For me switch( x+y ) is 2, also it should print default. x and y are both final! And I thought (x+y) is allowed in a switch expression?

Thanks for help.

because the switch evaluates the x+y =2 then  compiler search for a related value, case y: has the value 2, so it jumps to case y but not the default reason is that switch has the ability to create a jump statement  whatever the cases you have, so answer is case y exicuited which has the value 2 related to case x+y

Mike Gualeni wrote:In the interactive quiz, I choose exam 2 and this code segment is from question 50.

50. Given the following classes and interface, what ist the output of the code segment?


A. Status: 1 Error: false
B. Status: 2 Error: false
C. Status: 1 Error: true
D. Status: 2 Error: true
E. A compiler error will occur.
F. A runtime error will occur.

I choose solution A, but that was incorrect!!

The explanation is very short and I don't understand it!

E is correct. This will generate a compiler error because a superclass, ClassX, is trying to polymorphically act as a subclass, ClassY.

A, B, C, D, and F are incorrect. B is incorrect; however, if the variable sampleA was initialized as ClassY, it would be correct.

Why is superclass ClassX trying to polymorphically act as a subclass, ClassY?? That answer I don't understand!!! Can someone give me a simple explanation, if it exists one?
What if we add the interface method public boolean errorState();{ return true; } to the ClassY, would then the code compile?

Thanks in advance.

superclass reference can point to subclass object but not vise versa
Because  a subtype object can be substituted in the supertype reference
Whenever a subtype object is created a supertype object is created implicitly, polymorphic behavior can change the code at runtime with respect to object and also a subclass has some knowledge of superclass that is inherited
Second true does bother for your exicuition

Thiago Medeiros wrote:I'm in the chapter Concurrency of OCP study guide, and I saw this snipped:

This throws a ConcurrentModificationException.
Changing the instance type from HashMap to ConcurrentHashMap fix the problem, but it was not clear enough for me why the snippet throws that exception.
Any explanation will be welcome

Thanks in advance

you can use basic for loop that does not throw exception any hope it helps

Mike Gualeni wrote:Hi,

Why I don't get a StackOverflowError with the code:

Because you are catching the stackoverflowerror error in the catch block and using the for loop to display the values