<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Java 瞬態與靜態變量 > 原文: [https://javabeginnerstutorial.com/core-java-tutorial/transient-vs-static-variable-java/](https://javabeginnerstutorial.com/core-java-tutorial/transient-vs-static-variable-java/) 在這里,我將向您展示靜態和瞬態之間的區別。 無論如何,這兩件事在范圍上是完全不同的,但是人們經常會問我這個問題。 在本文的這篇文章中,我將嘗試對其進行解釋。 在之前的文章中,您可以在此處單擊[(第一部分)](https://javabeginnerstutorial.com/core-java-tutorial/java-serialization-concept-example/ "Java serialization concept and Example"),在此處單擊[以獲取(第二部分)](https://javabeginnerstutorial.com/core-java-tutorial/java-serialization-concept-example-part-ii/ "Java serialization concept and Example Part II")。 ## 靜態變量 靜態變量屬于一個類,而不屬于任何單個實例。 序列化的概念與對象的當前狀態有關。 僅與類的特定實例關聯的數據被序列化,因此靜態成員字段在序列化過程中將被忽略。 ## 瞬態變量 如果您不想保存變量的狀態,請在序列化時使用。 您必須將該變量標記為`Transient`。 環境將知道應忽略此變量,并且不會保存相同的值。 > 即使概念是完全不同的,并且不進行儲蓄的原因也不同,人們仍然對兩者的存在感到困惑。 與這兩種情況一樣,變量值將不會保存。 ## 兩者之間的區別 源代碼:注意每個代碼更改。 `Employee.java` ```java package com.jbt; import java.io.Serializable; public class Employee extends superEmployee { public String firstName; private static final long serialVersionUID = 5462223600l; } class superEmployee implements Serializable{ public String lastName; static String companyName; transient String address; static transient String companyCEO; } ``` `SerializaitonClass.java` ```java package com.jbt; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class SerializaitonClass { public static void main(String[] args) { Employee emp = new Employee(); emp.firstName = "Vivekanand"; emp.lastName = "Gautam"; emp.companyName = "JBT"; //Below part needs to be removed in case address field is made final emp.address = "MUM"; emp.companyCEO = "ME CEO"; try { FileOutputStream fileOut = new FileOutputStream("./employee.txt"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(emp); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in ./employee.txt file"); } catch (IOException i) { i.printStackTrace(); } } } ``` `DeserializationClass.java` ```java package com.jbt; import java.io.*; public class DeserializationClass { public static void main(String[] args) { Employee emp = null; try { FileInputStream fileIn = new FileInputStream("./employee.txt"); ObjectInputStream in = new ObjectInputStream(fileIn); emp = (Employee) in.readObject(); in.close(); fileIn.close(); } catch (IOException i) { i.printStackTrace(); return; } catch (ClassNotFoundException c) { System.out.println("Employee class not found"); c.printStackTrace(); return; } System.out.println("Deserializing Employee..."); System.out.println("First Name of Employee: " + emp.firstName); System.out.println("Last Name of Employee: " + emp.lastName); System.out.println("Company Name: "+emp.companyName); System.out.println("Company CEO: "+emp.companyCEO); System.out.println("Company Address: "+emp.address); } } ``` 首先執行“`SerializaitonClass`”,您將獲得以下輸出。 ```java Serialized data is saved in ./employee.txt file ``` 其次,執行“`DeserializationClass`”,您將獲得以下輸出 ```java Deserializing Employee... First Name of Employee: Vivekanand Last Name of Employee: Gautam Company Name: null Company CEO: null Company Address: null ``` 從輸出中可以看到,只有姓氏值被保存。 靜態和瞬態變量值均未保存。 現在,我將稍作更改代碼,看看會發生什么。 `Employee.java` ```java package com.jbt; import java.io.Serializable; public class Employee extends superEmployee { public String firstName; private static final long serialVersionUID = 5462223600l; } class superEmployee implements Serializable { public String lastName; /* * Here i am providing the value of company name,companyCEO and address * while defining these variables. */ static String companyName = "TATA"; transient String address = "DEL"; static transient String companyCEO = "Jayshree"; } ``` 再次執行相同的代碼并查看輸出 ```java Deserializing Employee... First Name of Employee: Vivekanand Last Name of Employee: Gautam Company Name: TATA Company CEO: Jayshree Company Address: null ``` 非常仔細地查看輸出。 此處已保存`companyName`,`companyCEO`的值,但未保存地址的值。 另外,檢查這些變量的保存值。 ```java Company Name: TATA Company CEO: Jayshree ``` 在這兩種情況下,此處存儲的`case`值均來自類(`Employee`類),而不是對象(`emp`對象)。 此外,即使是臨時變量,也會保存`companyCEO`變量值。 因為`static`修飾符會更改此變量的行為。 ## 重點 1. 靜態變量無法序列化。 2. 在反序列化時,如果在基類的初始化過程中提供了相同的值,則可用于靜態變量。 * 這并不意味著靜態變量將被序列化。 這僅意味著靜態變量將使用相同的值初始化,并在加載類時分配(在這種情況下為`TATA`)。 如果之前未加載類(新 JVM)。 請注意示例代碼。 * 如果類已經在 JVM 中加載并且靜態變量值已更改,那么將顯示更改后的值。 3. 如果將變量定義為“靜態”和“瞬態”,則靜態修飾符將控制變量的行為,而不是瞬態。 * 靜態和瞬態是不同的。 在某些情況下,它們的行為看起來相同,但并非總是如此。 如果在加載類時為變量分配了一個值,則在反序列化類時該值將分配給靜態變量,而不是瞬時的。 因此,如果您同時使用這兩個修飾符和變量,那么從某種意義上來說,我要說的是靜態將優先于瞬態。 4. 瞬態變量值將不會保存。 同樣,在反序列化過程中不能為其分配任何值。 這與靜態行為不同。 ## 最終修飾符對序列化的影響 要查看`Final`修飾符的效果,我再次更改`Employee`類的代碼。 `Employee.java` ```java package com.jbt; import java.io.Serializable; public class Employee extends superEmployee { public String firstName; private static final long serialVersionUID = 5462223600l; } class superEmployee implements Serializable { public String lastName; /* * Here i am providing the value of company name,companyCEO and address * while defining these variables. * I am making address as final here */ static String companyName = "TATA"; transient final String address = "DEL"; static transient String companyCEO = "Jayshree"; } ``` 再次執行代碼,看看區別。 上面代碼的輸出將是 ```java Deserializing Employee... First Name of Employee: Vivekanand Last Name of Employee: Gautam Company Name: TATA Company CEO: Jayshree Company Address: DEL ``` 如您所見,序列化過程中還保存了地址字段,因為它現在是最終的。 ## 接口和最終 我看到了一種情況,您可以序列化未序列化的接口內部的變量。 `Employee.java` ```java package com.jbt; import java.io.Serializable; public class Employee extends superEmployee implements variableConstant{ public String firstName; private static final long serialVersionUID = 5462223600l; } class superEmployee implements Serializable { public String lastName; /* * Here i am providing the value of company name,companyCEO and address * while defining these variables. * I am making address as final here */ static String companyName = "TATA"; transient final String address = "DEL"; static transient String companyCEO = "Jayshree"; } interface variableConstant { public static final String education = "MCA"; } ``` `DeserializationClass.java` ```java package com.jbt; import java.io.*; public class DeserializationClass { public static void main(String[] args) { Employee emp = null; try { FileInputStream fileIn = new FileInputStream("./employee.txt"); ObjectInputStream in = new ObjectInputStream(fileIn); emp = (Employee) in.readObject(); in.close(); fileIn.close(); } catch (IOException i) { i.printStackTrace(); return; } catch (ClassNotFoundException c) { System.out.println("Employee class not found"); c.printStackTrace(); return; } System.out.println("Deserializing Employee..."); System.out.println("First Name of Employee: " + emp.firstName); System.out.println("Last Name of Employee: " + emp.lastName); System.out.println("Company Name: "+emp.companyName); System.out.println("Company CEO: "+emp.companyCEO); System.out.println("Company Address: "+emp.address); System.out.println("Education: "+emp.education); } } ``` 執行以上代碼,然后查看輸出。 ```java Deserializing Employee... First Name of Employee: Vivekanand Last Name of Employee: Gautam Company Name: TATA Company CEO: Jayshree Company Address: DEL Education: MCA ``` 在這里,您可以看到教育的值得以保存。 此值是接口的一部分。 但是由于這是恒定的,因此在序列化時將其保存。 I think I have covered all possible scenarios. Do let me know in case any particular scenario is not covered in this article. Feel free to say me hi, ?if you like this article. And yes I didn’t qualify for the interview. ??
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看