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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:48:58+00:00 2026-06-15T12:48:58+00:00

Question is about Java . There are three ways of obtaining classes of a

  • 0

Question is about Java. There are three ways of obtaining classes of a type (note that code is just a collection of snippets for demonstration):

Class c1 = Class.forName("com.mypkg.MyClass"); //assumes initialize = true
// - OR - 
Class c1 = Class.forName("com.mypkg.MyClass", true/false,
                         this.getClass().getClassLoader());

Class c2 = com.mypkg.MyClass.class;
// - OR - 
import com.mypkg.MyClass;
Class c2 = MyClass.class;

MyClass mc = new MyClass();
Class c3 = mc.getClass();

My question is about initialization. For method 1, I can control whether class is initialized or not using the boolean argument. For method 3, since an object is created, obviously the class is initialized (right?).

But what about method 2? Does calling .class initialize the class if it has not already been initialized?
And how can one programmatically check whether a class is already initialized or not?

Summary answer:

Please see accepted solution and its comments for full answer. This is just a summary.

Calling .class does not initialize the class if it has not already been initialized. You can check whether a class is getting initialized by using a static block in the class definition that prints a message.

Reason for original question:

According to javadoc of Class.forName, “A call to forName(“X”) causes the class named X to be initialized.”. Also there are two Class.forName methods including one that accepts a boolean parameter called initialize. According to javadoc, “The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.”

  • 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-15T12:48:59+00:00Added an answer on June 15, 2026 at 12:48 pm

    How about testing it yourself, to see what your VM does?

    Just use this class with all three methods (in independent runs, as it will only be initialized once at most!)

    class Example {
        static {
            System.out.println("Class was initialized!");
        }
    
        public static int bananas = 0;
    }
    

    for details when a class is supposed to be initialized, see http://docs.oracle.com/javase/specs/#12.4.1

    Roughly speaking, a class is supposed to be initialized when either

    • the first instance is created
    • a static method is invoked
    • a non-final static field is used
    • some more complex corner cases

    So essentially, when you first use anything of the actual class, not just reference it.

    But e.g. Example.bananas = 1; should also trigger class initialization, without having an instance.

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

Sidebar

Related Questions

I have a question about local classes in Java (classes that declares in the
I asked a question earlier about ESBs written in Erlang or Java, and there
Just a quick question about ReadWriteLocks in Java (specifically the ReentrantReadWriteLock implementation) as I
I have a question about how GC works in Java. Consider the following code:
Naive question about Java syntax. What does <T> T accept(ObjectVisitorEx<T> visitor); mean? What would
General question about java servlets and the best way to handle requests. If I
I had a question about Java Class Path variables. If I have multiple jars
A question about inheritance in java... class Base { private int val = 10;
I posted a question last night about Java Reflection and am discovering compiler warnings
Thanks for your answers to my previous question about GUI in java . I

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.