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

  • Home
  • SEARCH
  • 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 4095294
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:52:33+00:00 2026-05-20T19:52:33+00:00

Spring newbie here. I observed that Spring was able to instantiate a non-public class

  • 0

Spring newbie here.

I observed that Spring was able to instantiate a non-public class (i.e. a class with default visibility) that I had defined. Can anyone tell me how Spring achieves this? Why is this allowed ?

  • 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-05-20T19:52:33+00:00Added an answer on May 20, 2026 at 7:52 pm

    OK, here’s how they do it. Take this sample class:

    package hidden;  
    
    class YouCantInstantiateMe{
    
        private YouCantInstantiateMe(){
            System.out.println("Damn, you did it!!!");
        }
    
    }
    

    The above is a package-private class with a private constructor in a different package, but we’ll still instantiate it:

    Code (run from a class in a different package):

    public static void main(final String[] args) throws Exception{
        Class<?> clazz = Class.forName("hidden.YouCantInstantiateMe");
                                                // load class by name
        Constructor<?> defaultConstructor = clazz.getDeclaredConstructor();
        // getDeclaredConstructor(paramTypes) finds constructors with
        // all visibility levels, we supply no param types to get the default
        // constructor
        defaultConstructor.setAccessible(true); // set visibility to public
        defaultConstructor.newInstance();       // instantiate the class
    }
    

    Output:

    Damn, you did it!!!


    Of course what Spring does is much more complex, because they also deal with Constructor Injection etc., but this is how to instantiate invisible classes (or invisible constructors).

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

Sidebar

Related Questions

newbie doing Java homework here. I have one class named Album which contains the
quite a newbie here but here's a small test code that explains my issue.
Newbie question here! I'm building a simple application that allows users to create and
Total newbie here, please bear with me. Building a very small personal app that
Complete C# newbie here, so cut me some slack. I have this application that
I am newbie in spring. using spring 3.0 mvc. I am creating a spring
I'm a newbie in Spring Batch. I have inherited a batch process implemented with
Spring support JUnit quite well on that: With the RunWith and ContextConfiguration annotation, things
I am newbie to ajax here and I know somebody would have encountered this
Java newbie here, I'm having trouble setting a new line in this code: String

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.