Tuesday, April 20, 2010

Static vs Transient variables

Static variables
  • Static variables are class variables.
  • Static variables can be serialized.
  • static modifier applies to static variables.
       class TestClass{
          static int testVariable;
          . . .
      }

Transient variables
  • Transient variables are member or instance variables of class.
  • Transient variables can not be serialized.
  • transient modifier applies to transient variables.
       class TestClass{
          transient int testVariable;
          . . .
      }

2 comments:

  1. yes.....got it!
    I have a question regarding volatile modifier.
    I heard it can b used in timer applications,but
    even after reading much about it i am not able to understand correctly.
    Can u please explain with appropriate example..

    ReplyDelete
  2. Hi Kapish,

    The volatile is not necessary to used only in timer applications. The below post may help you to understand more about volatile modifiers.

    http://sharad-java-j2ee-interview-questions.blogspot.com/2010/05/volatile-modifiers-in-java.html

    If you still have problem please let me know.

    Have a great day :)

    ReplyDelete