Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which situations are possible the use of transient, synchronized, native and protected modifiers?

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which situations are possible the use of transient, synchronized, native and protected modifiers?

thanks,
AS
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I've just read, Transient is used for variable you wish to not be persisted in the sense of being serialized (where the variable is converted to a stream of bytes and stored to a file). The synchronized keyword protects code from being executed by more than one thread at a time. Protected is (I believe) used as a keyword for variables that can only be used through inheritance.

If you google these you will probably find actual code snippet of how these are used as well.

Justin
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But could you give any exemple please?

Really thanks for helping...

AS

 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
André Asantos wrote:But could you give any exemple please?

You'd probably get the answer faster by searching for it yourself. You can always then come back here, post the example, and say "I don't understand something here..."

We generally prefer folks showSomeEffort, rather than just asking for answers to homework problems.
 
Masa Saito
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
André Asantos wrote:Which situations are possible the use of transient, synchronized, native and protected modifiers?

thanks,
AS

here is a situation where you need: transient, synchronized, and protected. [but not "native"]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Fox wrote:Protected is (I believe) used as a keyword for variables that can only be used through inheritance.
The keyword protected allows access in the same package and in subclasses. Not quite "through inheritance." You have obviously been taught by somebody who thought it means the same as in C++ where (I believe) it does mena "thorugh inheritance."
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Masa,
thanks a lot for you attention... But sorry I did not understand your answer...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
André Asantos wrote:Hi Masa,
thanks a lot for you attention... But sorry I did not understand your answer...
Nor did I.
 
Daia Yum
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a member variable in your class such that it is accessible in any class in the same package your class belongs to or in other subclasses of your class in any other package, use Protected access modifier.
 
Masa Saito
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
André Asantos wrote:Hi Masa,
thanks a lot for you attention... But sorry I did not understand your answer...
Nor did I.

As the class implements Serializable, and and open Socket is "stateful" the Socket would need to be transient.
Writing to the socket's stream would require synchronization (that there are two methods that write is not relevant). the write and the incriment of "x" needs to be atomic. sync-ing only on the output stream would be best. To implement the Factory pattern, you can't have public constructors so you'd need to make the constructor protected. the subclasses would need to have the static factory methods. as static methods can't be abstract, i had to write in the comments the class needs extending.

anyway. take care.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic