aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is trasient + static valid ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "is trasient + static valid ??" Watch "is trasient + static valid ??" New topic
Author

is trasient + static valid ??

shubham garg
Greenhorn

Joined: Jun 22, 2009
Posts: 17
does the 'transient' modifier used with 'static' form a legal declaration ?? ...

thanks and regards
shubham


preparing for SCJP 6
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
shubham garg wrote:does the 'transient' modifier used with 'static' form a legal declaration ?? ...

thanks and regards
shubham

As far as i understand, statics are implicitly transient, as their state is not a part of state of an object, but a part of state of the class itself. So static vars don't serialize with instance variables. And since code with "transient static int myVar=0;" compiles well, yes, it is legal, but behaves the same way as just "static int myVar=0;" Correct me if i am wrong.


[SCJP 6.0]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

shubham garg wrote:does the 'transient' modifier used with 'static' form a legal declaration ?? ...

thanks and regards
shubham


Did you try ? What happened when you did ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Actually this is really tricky..... As statics are not at all valid for serialization as serialization is for objects an statics are not object variables...... but transient variables are object variables but we make it transient because we don't want eh value to be recorded or saved and also we make it transient for a object reference if that class is not implementing serializable.

Well i think transient variables are instance variables which are not used for serialization.

The java compiler does not complaint if you declare a static member field as transient. However, there is no point in declaring a static member field as transient, since transient means: "do not serialize", and static fields would not be serialized anyway.

Its like static variables are transient but transient variables cannot always be static. its more like the equals and hashcode thing


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

well thing with static and transient is ok..... What will happen if the variable is transient final and more over if the variable is transient static final?
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Nitish Bangera wrote:well thing with static and transient is ok..... What will happen if the variable is transient final and more over if the variable is transient static final?

What transient does - it says compiler that this particular variable doesn't go to serialized form. So for this mechanizm no matter if the variable final or not - you either want to serialize you final object variable or you don't want this variable to be a part of serialized state of your object.
So is absolutely legal.

And if the variable is static - it just is not serializable transient or not (statics kinda implicitly trancient). So is also legal, but you can omit the keyword "trancient" with the same result
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is trasient + static valid ??
 
Similar Threads
static and non static methods
static and non static methods
property file dynamic vs static
Single tone Vs Static
WA #1.....word association