• 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

Transient Var

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can a variable which is Transient be declared both static and final?
Simon roberts

Transient variables may not be static or final


i tried declaring a variable as follows:
public transient static final int _FRAMEX = 850;
it worked without any error.
is the statement in the simons roberts book correct or incorrect?
help me on this.
thanks
zarina
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
transient keyword means that the variable's value is not stored during serialization, i.e. not persistant. so it will keep changing. by declaring it as final, you counter-effect the transient keyword.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by zarina mohammad:
Can a variable which is Transient be declared both static and final?
Simon roberts
i tried declaring a variable as follows:
public transient static final int _FRAMEX = 850;
it worked without any error.
is the statement in the simons roberts book correct or incorrect?
help me on this.
thanks
zarina


According to the book "Mike Meyers' Certification Passport - Java 2" there is a chart on page 23 which lists Field Modifier Cominations and it shows that you can make a variable final (thus making it a constant) and you can make it static (belonging to the class) and you can make it transient (meaning it won't be saved during object serialization.
So it looks like it would be "legal" as you successful compilation demonstrates but one would seem to have a good reason for not saving a constant definition that belongs to the whole class when you serialize the class ???
[ July 05, 2002: Message edited by: david eberhardt ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amir Ghahrai:
transient keyword means that the variable's value is not stored during serialization, i.e. not persistant. so it will keep changing. by declaring it as final, you counter-effect the transient keyword.


I agree that the use of transient and final appear to be contradictory. However, one could argue that the transient keyword is frequently used for security purposes and even constant values may sometimes be sensitive. Information that should not be serialized can be declared transient to prevent the information from being transmitted to an unsecure location. One could argue that even a constant value might sometimes be sensitive and therefore it may be necessary to deny the serialization of that value. Although it may be difficult to come up with a good example, it would be even more difficult to prove that none exists.
 
zarina mohammad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all for the responses.
so what can be concluded here is that:
declaring a transient variable as static and final is not illegal though but contradictory to have a variable as both final static and transient at the same time.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zarina,
A variable that is declared both final and transient is simply a constant value that should not be serialized for transmission to another location. Although it is difficult to think of a good reason for doing that, it is more difficult to say that no reason can exist.
It is probably safe to say that useful applications for a final transient variable are limited.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'transcient' doesn't contradict 'final', so they
can both put together.
'transcient' doesn't contradict 'static' too, but the reasons of putting them together doesn't
make sense and results in an irrational association. The compiler sometimes allows an irrational association. In this case 'transcient'
is superfluous and has no effect. But sometimes
the compiler doesn't allow irrational association. For example, constructors cannot be
final because it is not supposed to be inherited. There is no harm if a constructor is final, but the compiler just doesn't allow you to do that.
We have to bear the strange character of the
compiler and treat the compiler as if it is a
human being because it is human being who makes
compiler so compilers all have some human character---inconsistence.
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by zarina mohammad:
Can a variable which is Transient be declared both static and final?


As I move around this forum, I learn more and more each day. In response to another question, someone noted the following reference which is on-line:
The JavaTM Language Specification, Second Edition
here are the rulesfor Field Modifiers:

8.3.1 Field Modifiers
FieldModifiers:
FieldModifier
FieldModifiers FieldModifier
FieldModifier: one of
public protected private
static final transient volatile
The access modifiers public, protected, and private are discussed in �6.6. A compile-time error occurs if the same modifier appears more than once in a field declaration, or if a field declaration has more than one of the access modifiers public, protected, and private.

If two or more (distinct) field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for FieldModifier.


.....
Therefore -
1) the Field can only be one of public, protected, private, ( or default, which is blank) and,
2) you could declare it all of the following;
static final transient volatile
------------------------------------------------
 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more note to my last post,
Section 8.3.1.4 volatile Fields says the following -

A compile-time error occurs if a final variable is also declared volatile.


(so you can't use all four field modifiers at one time since volatile and final are incompatible)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic