GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Which situations are possible the use of transient, synchronized, native and protected modifiers? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Which situations are possible the use of transient, synchronized, native and protected modifiers?" Watch "Which situations are possible the use of transient, synchronized, native and protected modifiers?" New topic
Author

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

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Which situations are possible the use of transient, synchronized, native and protected modifiers?

thanks,
AS
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
But could you give any exemple please?

Really thanks for helping...

AS

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11352
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Masa Saito
Greenhorn

Joined: Jun 08, 2010
Posts: 14
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

Joined: Oct 13, 2005
Posts: 39070
    
  23
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

Joined: Nov 23, 2009
Posts: 234
Hi Masa,
thanks a lot for you attention... But sorry I did not understand your answer...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39070
    
  23
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

Joined: Mar 23, 2009
Posts: 19
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.


Daia
Masa Saito
Greenhorn

Joined: Jun 08, 2010
Posts: 14
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.
 
GeeCON Prague 2014
 
subject: Which situations are possible the use of transient, synchronized, native and protected modifiers?