This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!

Tom Mark

+ Follow
since Jun 15, 2011
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 Tom Mark

When we execute a code sometimes we get a result followed by an exception and sometimes an exception is thrown without any output. In both case we have something in the code that has to be printed.
Can anyone explain who determines it?
Why does the method below compiles?

and these two don't?

I know that when you call the method, you can declare V to be anything at all. And it's not ok.
But what about this method:

T can be B or C, but the argument of this method can be anything C, B, A and also Object because of "super" keyword (Letter<? super T> t) .
This is not within the bounds of the class declaration (public class Letter<E extends B>{}) .
Can you explain how the compiler works?
I've got confused. I know that init block executes before constructor, but I don't understand in what order they run separately in Superclass and subclass when code has methods or variables?
You' ve said that in XClass instance blocks run in the order that they appear. It means that {s="C++";} run first and if it was printed after String s="Java"; it would have executed after String statement.
Doesn't it contradict the rule which tells that init block executes before constructor and the rest of the code?

Again I'm trying to understand the sequence how does the code executes. Correct me if I'm wrong.
For example we have a class with one init block, and one method. As I know all classes has implicit constructor. It means that when you instantiate an object the code in an init block runs first.
Then JVM executes constructor if it is explicit and has some code inside and only then JMV runs code inside method.
Everything is ok when I look at a YClass. But when I look at the XClass I see that init block runs in the order that they appear, but not before constructor and the rest of the code.

You are saying that instance blocks run in the order that they appear. But what if we add one more init block {s="C#";} in YClass.

According to the previous rule the result should be DemoC++ , cause int block runs after the main method in YClass .
But JVM says that the result is DemoC#. Why?

I think I have missed something learning about init blocks or constructors. Can anyone explain me the order in which executes XClass?
Assume we have a code

I know that the result will be DemoC++. But what if we change the location of init block in XClass.

Why the result is DemoJava?
Get it.
Thank you all.
But we can write

and we'll get the same result. Why do we need a?

And what if we try to invoke a.doTask1() method by different threads?
I'm trying to figure out how statement

affects accessibility of object "a" which has method doTask1();
Sorry, I still don't get it.

Line synchronized(a) locks only A object which means that two threads can't access method void doTask1 concurrently, but they can
access code concurrently in the following code:

because the lock is on A, not on synchronized(this)
That means that the result could be 112233 or else.

I know that I'm wrong, but I don't know why.
Please explain.
The result of the following code is always: 1 2 3 1 2 3
WHY? I thought the result is unpredictable.
how does synchronized blocks works? I thought synchronized(a) line locks only object on reference "a", but it lock on "b" too.

ok. I'll write what I have just understood. Correct me if I'm wrong.

The code fragment from my last message means that..

.. there are two threads with two different instances(new Chess()). Each instance has its own method run() and executes it and calls method move(). Threads don't share these methods with each other.

This code fragment has two threads but one instance(runnable). It executes the same method run() on the same instance. But run() method owns methods with different instances.

As I see everything depends on what do we have in a run() method. Am I wrong?

Eduardo Mineo wrote:Hi Tom.

When I was getting myself prepared to OCJP, I realized that confusion is the aim of the test. So, get used to it

In fact, you must pay attention for all instance in play. Ever. But, in this case, pay even more attention to "static Dudes d". Only one instance of Dudes is in play, so you have two threads, represented by two instance of DudesChat making calls to only one instance of Dudes, created only one time on d = new Dudes().

Can you see the difference?


I see that both threads shares the same method on the same instance "new Dudes" which was instantiated only once cause it is static.

The next code also shows that two threads shares same methods x.a(), y.a() which are with different instances.
Here another one which confuses me.

Don't understand the following statement:

Threads calling non-static synchronized methods in the same class will
only block each other if they're invoked using the same instance and if
they're called using two different instances, they get two locks, which do not interfere with each other.

To which instance do I have to pay attention for?

As I understand the code above has two different instances and according to previous rule instances should get two locks, which means that Threads do not interfere with each other.

But some guys in forum says that method is called on the same instance by both the threads...

Suppose they are right. But what about this code?

The following Threads have one instance "runnable"

But SCJP test says that methods x.a() in thread1, and y.a() in thread2 are called on different instances and they
do not block each other.

So which instance determines whether to block or not???
Thank you Stephan for your time.
You helped a lot.
Ok. But lets go back to the first method. This will be my last question.
As I understand, the method below doesn't redeclare K too.
It uses K (not <K>) declared in the class signature. Am I right?

You mention earlier that we can state <K> explicitly. So it's clear.
But what happens if don't state explicitly. For example:

What will be the return type?