Question

Question 2 [8 points] Consider the class Class A below: class Class A implements Serializable{ int a; static int b; transient

the answer must be in java. thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.io.*;

class classA implements Serializable {
   int a;
   static int b;
   transient int c;
   String s;
  
   public classA(int a, int b, int c, String s)
   {
       this.a = a;
       this.b = b;
       this.c = c;
       this.s = s;
   }
}

class Test {
   public static void printdata(classA objectA)
   {
       System.out.println("a = " + objectA.a);
       System.out.println("b = " + objectA.b);
       System.out.println("c = " + objectA.c);
       System.out.println("s = " + objectA.s);
   }

   public static void main(String[] args)
   {
       classA A = new classA(10, 100, 1000, "10000");
       String fileName = "fileClassA.txt";

       // Serialization code
       try {
           System.out.println("Before Serialization:");
           printdata(A);
           FileOutputStream fos = new FileOutputStream (fileName);
           ObjectOutputStream out = new ObjectOutputStream(fos);
           out.writeObject(A); //writes the byte stream to file
           out.close();
           fos.close();
       }
       catch (Exception e) { //To catch IOException
           e.printStackTrace();
           System.out.println(e);
       }

       A = null;
       // Deserialization
       try {
           FileInputStream file = new FileInputStream (fileName);
           ObjectInputStream in = new ObjectInputStream (file);
           A = (classA)in.readObject(); //read byte stream from file and typecast to required class
           in.close();
           file.close();
           System.out.println("After Deserialization:");
           printdata(A);
       }
       catch (Exception e) { //To catch IOException or ClassNotFoundException
           e.printStackTrace();
           System.out.println(e);
       }
   }
}


Output:

Serialization: Converting state of an object into a byte stream. Uses writeObject()

Deserialization: Byte stream recreates the actual Java object in memory. Uses readObject()

Transient variables and Static variables cannot be serialized, so when transient variable is deserialized it is initialized as 0 or NULL (default value of object) and static variable value will loaded from the class

Add a comment
Know the answer?
Add Answer to:
the answer must be in java. thank you Question 2 [8 points] Consider the class Class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the Triangle class (from previous code, will post under this) to throw an exception in...

    Modify the Triangle class (from previous code, will post under this) to throw an exception in the constructor and set routines if the triangle is not valid (i.e., does not satisfy the triangle inequality). Modify the main routine to prompt the user for the sides of the triangle and to catch the exception and re-prompt the user for valid triangle sides. In addition, for any valid triangle supply a method to compute and display the area of the triangle using...

  • can someone explain to me what does the dot mean ? and what would be the...

    can someone explain to me what does the dot mean ? and what would be the output and explain it? import java.io.*; public class Green { private int a; private int b; public Green(int aa,int bb) { a=aa; b=bb; } public void equals(Green c) { this.a=c.a;this.b=c.b;} public void fn(Green c) { this.a=3*c.b-c.a; this.b=2*c.a-this.b;} public void gg() { this.b=this.b-1; this.a=this.b-2; } public static void main(String args[]) { Green x=new Green(2,2); Green y=new Green(2,1); Green z=new Green(1,4); int xx=1,yy=2,zz=3; x.fn(y); z.gg(); System.out.println("...

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • Show the output of running the class Test in the following code lines: interface A {...

    Show the output of running the class Test in the following code lines: interface A { void print (); } class C ( class B extends C implements A { public void print() { } } class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); w class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • Given the following class: class Q2 { private int a; private int b; private int c;...

    Given the following class: class Q2 { private int a; private int b; private int c; public void setA(int a){this.a = a; } public void setB(int b){this.b = b;} public void setc(int c){this.c = c;} public int geta(){return a; } public int gets(){return b;} public int getc(){return c;} public int m1(int a, int b){ return a + b; public boolean m2 (int x, int y){ return m1(x, y) + x + y < 10; What is the output of the...

  • Abstract classes and Interfaces problems 10. Explain one similarity and one difference between ab...

    Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...

  • Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and...

    Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...

  • What is the output of the following code? Explain your answer. public class Test {public static...

    What is the output of the following code? Explain your answer. public class Test {public static void main (String [] args) {System.out. print((int) 5.6); System.out.println((int) 5.2);}}

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT