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 8150555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:05:49+00:00 2026-06-06T15:05:49+00:00

I noticed that in Java, you can use a class which has members of

  • 0

I noticed that in Java, you can use a class which has members of types that are unavailable to you. For example, say you have a precompiled .class file for the following Person class; but not for Address, which it uses:

public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    private String sex;
    private String name;
    private transient Address address;

    public Person(String sex, String name) {
        this.sex = sex; this.name = name; 
    }
    public void setAddress(Address address) {
        this.address = address
    }
}

You can instantiate this class with Person person = Person("Male", "Andrew"); even if the type Address is not available to your Java project.

Now the problem is, if you want to serialize a Person object, you cannot do this unless the Address type is available (i.e. can be loaded by the class loader), even though the Address field is transient.

For some reason Java needs to know the type Address even though it will not need to serialize it…

Any help on how I could serialize Person anyway without having to redefine a Person class without an address? And any explanation of why Java needs to know the type of address although it will not need to serialize it?

Many thanks.

  • 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-06T15:05:50+00:00Added an answer on June 6, 2026 at 3:05 pm

    I am operating under the assumption that you have a .class file for Person, but no .class file for Address. (Perhaps Person is a dependency from a different project?)

    The reason you cannot serialize Person (even though the Address field is transient) is that the serialization API uses the Java reflection API. Although the JVM will load a class without loading all of its dependencies, the reflection API is not as forgiving.

    The first time the reflection API is used to retrieve field information for a particular class, it retrieves and caches information for all fields in the class. To do this, it has to resolve the types for every field in the class, and thus will attempt to load the class files for every field. (You can see this in the Java source code: ObjectOutputStream uses ObjectStreamClass, which calls Class.getDeclaredField, which calls a private method privateGetDeclaredFields, which resolves and caches all the field definitions for the class.)

    As a workaround, if your code never actually uses any objects of type Address, you can simply create an empty Address class in the correct package, compile it, and add it to your class path:

    public class Address { }
    

    For those who think it isn’t possible to use Person at runtime without an Address class definition, the following three classes demonstrate what the OP is talking about:

    Person.java

    import java.io.Serializable;
    public class Person implements Serializable {
        private String name;
        private transient Address address;
        public Person(String name)  { this.name = name; }
        public Address getAddress() { return address; }
        public String getName()     { return name; }
    }
    

    Address.java

    public class Address {
        private String address;
        public String getAddress()  { return address; }
    }
    

    Test.java

    import java.io.FileOutputStream;
    import java.io.ObjectOuputStream;
    public class Test {
        public static void main(String...args) throws Exception {
            Person person = new Person("John Doe");
            System.out.println("Person successfully instantiated with name " + person.getName());
    
            // now attempt to serialize
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.out"));
            out.writeObject(person); // NoClassDefFoundError thrown here if Address.class doesn't exist
            out.close();
        }
    }
    

    Now compile Test.java, delete Address.class, and run Test:

    $ javac Test.java
    $ rm Address.class
    $ java Test
    Person successfully instantiated with name John Doe
    Exception in thread "main" java.lang.NoClassDefFoundError: LAddress;
            at java.lang.Class.getDeclaredFields0(Native Method)
            at java.lang.Class.privateGetDeclaredFields(Class.java:2308)
            at java.lang.Class.getDeclaredField(Class.java:1897)
            at java.io.ObjectStreamClass.getDeclaredSUID(ObjectStreamClass.java:1624)
            at java.io.ObjectStreamClass.access$700(ObjectStreamClass.java:69)
            at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:442)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:430)
            at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:327)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1130)
            at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
            at Test.main(Test.java:10)
    Caused by: java.lang.ClassNotFoundException: Address
            at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
            ... 12 more
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have noticed in Java that you can have a function with object... as
I have a Java class which can be called from shell. (via java [command][options])
I just noticed that java.beans.Introspector getBeanInfo does not pickup any superinterface's properties. Example: public
I noticed that the launching of Java applets using deployJava.js seems to have stopped
I have noticed that numerous Date methods in Java have been deprecated, but rather
I use Hakyll to generate some documentation and I noticed that it has a
In Java I use getters/setters when I have simple models/pojos. I find that the
I just installed Eclipse Galileo for Java developers and noticed that the update site
I noticed in the Java 6 source code for String that hashCode only caches
I noticed that most (all?) .winmd files have a version of 255.255.255.255 like: Windows,

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.