Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:56:22+00:00 2026-06-01T14:56:22+00:00

Foo.java public class Foo{ public int i = 0; } Bar.scala class Bar() extends

  • 0

Foo.java

public class Foo{
  public int i = 0;
}

Bar.scala

class Bar() extends Foo with Serializable{
  i = 1 
}

Serialization via Josh Seureth https://stackoverflow.com/a/3442574/390708

import java.io._

  class Serialization{
    def write(x : AnyRef) {
    val output = new ObjectOutputStream(new FileOutputStream("test.obj"))
    output.writeObject(x)
    output.close()
  }

  def read[A] = {
    val input = new ObjectInputStream(new FileInputStream("test.obj"))
    val obj = input.readObject()
    input.close()
    obj.asInstanceOf[A]
  }
}

REPL session, bar is 1 before serialization but 0 after.

scala -cp .
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_03).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val bar = new Bar
bar: Bar = Bar@2d2ab673

scala> bar.i
res0: Int = 1

scala> :load Serialization.scala
Loading Serialization.scala...
import java.io._
defined class Serialization

scala> val serialization = new Serialization
serial: Serialization = Serialization@41a45f89

scala> serialization.write(bar)

scala> val bars = serialization.read[Bar]
bars: Bar = Bar@5a9948fd

scala> bars.i
res3: Int = 0

So, why isn’t bars.i 1 in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:56:23+00:00Added an answer on June 1, 2026 at 2:56 pm

    This is expected and I believe has nothing to do with Scala. Non-Serializable superclasses are not serialized out (as they are not serializable!) and thus their values will be initialized by the default constructor.

    If you want to somehow save the superclass, you’ll need to override your readObject and writeObject to save the state manually. Or, use a more flexible serialization solution that writes XML, JSON, etc. and uses reflection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Foo implements java.io.Serializable { private int v1; private static double v2; private
In Java, I can pass an object straight in parameter public int foo (Bar
My code is something like: public class Foo { public int a; Bar[] bar
Let's say I have an abstract class: abstract class Foo extends Bar { public
Consider the following Java class: public class Foo { public static void doStuff() {
Let's say I have a regular simple Java class, such as: public class Foo
So in java, say you have a non-static method 'bar()' in an class 'Foo'.
Take this example: public class foo { public int[] func() { int arr[] =
I'm trying to do something like this in Java: public static <T> T foo()
I saw a java function that looked something like this- public static<T> foo() {...}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.