15

I have an issue related to the transient keyword's use before the private modifier in java .

variable declaration:

transient private ResourceBundle pageResourceBundle; 

My class looks like this :

public class LoginViewModel extends AbstractViewModel {

    transient private ResourceBundle pageResourceBundle;

    @AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
        initializeLoginValues();
        boolean timeout = BooleanUtils.toBoolean(getHttpServletRequest().getParameter("timeout"));
        if (timeout) {
            Messagebox.show(pageResourceBundle.getText("MSG_SESSION_HAS_EXPIRED_PLEASE_LOGIN"), pageResourceBundle.getText("LABEL_ALERT"),
                    Messagebox.OK, Messagebox.ERROR);
        }
        view.getPage().setTitle(CsdcLicence.get().getApplicationName());
    }

I have some questions.

1.why use the transient keyword before a private variable?

2.what is the purpose of using this keyword?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Sitansu
  • 3,225
  • 8
  • 34
  • 61
  • 8
    The link you provided answers both your questions. What is your doubt? What was not already covered? – Peter Lawrey Dec 20 '13 at 09:43
  • @PeterLawrey Edit my question pls see my class its not implements serialize – Sitansu Dec 20 '13 at 10:23
  • It is possible your class gets serialized by the library you are using or `transient` could be used by the library you are using with the model for another purpose. It might display all your fields in the view except the `transient` ones. You would have to read the documentation for the library you are using. – Peter Lawrey Dec 20 '13 at 10:45
  • Could someone please explain WHY would we want to intentionally lose the object over the network? Any examples? – Mugen Sep 09 '16 at 08:54

4 Answers4

33

transient variables are never serialized in java.

It marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred, they are lost intentionally.

please have a look at what serialization is.? and also refer this

Example

public class Foo implements Serializable
{
  private String saveMe;
  private transient String dontSaveMe;
  private transient String password;
  //...
}

In above example dontSaveMe & password are never get serialize as they are declare as a transient variables.

rachana
  • 3,344
  • 7
  • 30
  • 49
  • 5
    'Never' is too strong. They are not serialized *by default,* but if you write your own `readObject()/writeObject()` methods you can serialize them yourself. – user207421 Jul 06 '17 at 01:17
22

And a short use - case:
Imagine exposing a User - Object via a public available webservice. You sure would like to expose things as nickname, online - state, maybe email or location. You definitly would not want to expose the password the user uses to login. Whilst this password could be a property of your User- object, it should not be serialized e.g. when serializing the object to a JSON - String for the webservice mentioned.

Peter
  • 1,769
  • 1
  • 14
  • 18
7

transient keyword suggests that the object should not be serialized, nor persisted. You can use it if you don't want to serialize heavy objects (such as Wrappers, for example, which can contain a lot of business logic).

@Transient annotation suggests that the object should not be persisted (if you've been playing with Hibernate, for example), but it can be serialized.

I've included the annotation explanation, because I remember being confused by the two. :-)

Georgian
  • 8,795
  • 8
  • 46
  • 87
  • 1
    `transient` is only about serialization and its meaning is precisely specified by the JLS. `@Transient` is a 3rd-party defined annotation, and without giving the fully qualified classname it is ambiguous. – Marko Topolnik Dec 20 '13 at 09:49
3

transient is used to specify which properties of an object will not be saved or serialised. For example, when saving an object to a file, transient specifies which properties or attributes will not be saved to that file when that object is saved to a file.

When the object is re-created from the file, the value of that transient attribute (or private property) will not be re-created as it was never saved, or serialised to that file. In some cases, you may want to avoid persisting some of these private instance variables or attributes of an object, transient allows you to do that.

blackpanther
  • 10,998
  • 11
  • 48
  • 78