• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question regarding to thread in Chapter 9 of SCJP study guide

 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In chapter 9 of SCJP study guide published by McGrawHill, on P.741, it mentions "Or a static method that access a non-static field (using an instance)?"

The paragraph was talking about modifying a non-static field of a class via a synchronized static method. The class object is locked. But there is another thread running another synchronized non-static method trying to modifying the same non-static field. Since synchronized static and non-static methods are not blocking each other, they may modify the same non-static field at the same time, which is not thread safe.

But the issue is a synchronized static method cannot access a non-static field.
For example,

class MyRunnable implements Runnable{
private int a = 0;
public void static access(){
a = 100; // compile error: cannot make a static reference to a non-static field
}
}

But if this case:
class MyRunnable implements Runnable{

public void static access(){
AnyObject any = new AnyObject(); //assume there is a class AnyObject and there is an integer aNumber inside it.
any.aNumber = 100; // This any object is a local copy of the access method. Two threads have their own local copy. The modification won't affect each other.
}
}

So, please explain why "a static method that access a non-static field (using an instance)?" A static method accessing a non static field won't even compile.
Thanks.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a static method doesnt belong to any particular object.It belongs to class as a whole.

class MyRunnable implements Runnable{
private int a = 0;
public void static access(){
a = 100; // compile error: cannot make a static reference to a non-static field
}
}



in the above code with which object refence the field a is being accessed?? nothing, it is getting directly accessed which is not possible.

but if we take your second example

class MyRunnable implements Runnable{

public void static access(){
AnyObject any = new AnyObject(); //assume there is a class AnyObject and there is an integer aNumber inside it.
any.aNumber = 100; // This any object is a local copy of the access method. Two threads have their own local copy. The modification won't affect each other.
}
}




the field aNumber is not getting accessed directly in the static method,it is being accessed with the help of reference variable any.
I hope you got the point
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. A static method can be called by using an instance such as anInstance.staticMethod(). // assume there is an object anInstance.
This sounds strange, but it works. Usually, it should be called like this AClass.staticMethod(), assume we have a class called AClass.
In the SCJP exam, both method invokation are valid. This is the tricky part of the exam.

The static method can be accessed by two threads, but may need to be synchronized as we don't want two threads modifying the same static variables inside the static method.

If you read the SCJP study guide published by Kathy Sierra and Bert Bates, there is a sentence " Static method that access a non-static field."
This sentence is very confusing to me. How can a static method access a non-static field? This will be a compilation error.
I hope the author of this book will explain this paragraph on p. 741.
 
poorvika chanda
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi
I read the paragraph in Page 741 of Kathy sierra java 1.6.

Actually there is slight miscomunication

If you read the SCJP study guide published by Kathy Sierra and Bert Bates, there is a sentence " Static method that access a non-static field."



Please go to the previous two sentences before the above sentence in the paragraph.They are as follows.

However—what if you have a non-static method that accesses a static field?
Or a static method that accesses a non-static field (using an instance) ?

see the text marked in red colour.Author clearly mentioned static method accessing non static field using instance.
in the next sentence author just forgot to mention the word using instance.
that is it.
Regards
Poorvika
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic