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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:03:25+00:00 2026-06-09T14:03:25+00:00

I found this code online where the person is instantiating a class which has

  • 0

I found this code online where the person is instantiating a class which has a private constructor outside the class. I am not able to understand how this works, cause from what I had read earlier, you use a private constructor in a class so that it cannot be instantiated outside the class.

public final class A extends B {
  private A(Something, Something)
  {
    //Something
  }

  public void someMethods()
  {
    //Something
  }
}

public final class B {
  private A a;
  public void someMethod()
  {
    final ObjectInputStream objectInputStream = new ObjectInputStream(
                            new ByteArrayInputStream(buffer.toByteArray()));
    a = (A) objectInputStream.readObject();
    objectInputStream.close();

    a.someMethods();
  }
} 

I just want to understand what exactly is going on here? I tried reading up on ObjectInputStream but could not get anything out of it.

  • 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-09T14:03:27+00:00Added an answer on June 9, 2026 at 2:03 pm

    Well, the constructor is never called since the object is not being constructed but deserialized. Thus the access modifier has no effect here.

    Serialization is basically the process of writing part of the content of the heap into some stream/file etc. Constructing an object will create a new object on the heap and call the constructor to initialize it. It then has a state which is retained during serialization. Hence there’s no need for initializing that object again when it is deserialized. It’s just like reading that part of the heap from a stream/file into memory again.

    Besides that, keep in mind that there are ways to circumvent access modifiers by using reflection.

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

Sidebar

Related Questions

I found this code here class Usable; class Usable_lock { friend class Usable; private:
I found this code online that has a procedure inside a procedure. I cannot
I found this code using Google. private int RandomNumber(int min, int max) { Random
I found this nice snippet of code online: rkApp = Registry.LocalMachine.OpenSubKey(SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run, true); Which runs
I found this code online and tweaked it a bit for my need to
I am working with some code I found online: def person_path(options) # This is
I found this code online: def merge(left, right): result = [] i ,j =
i found this code online and im trying to compile it, but gcc keeps
I found this code online for shuffling array elements, it works well, but I
I found some code online which will move a box across the screen, then

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.