Results 1 to 6 of 6
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Initialization of Classes ?

    From "Thinking in Java":
    Itís interesting to note that creating a reference to a Class object using ".class" doesnít automatically initialize the Class object.
    When I try this:


    Java Code:
    package myPackage1;
    
    class Toy
    {
    	Toy(){};
    }
    
    
    public class ToyTest 
    {
    	static void printInfo(Class cc)
    	{
    		System.out.println("Class name: " + cc.getName()
    				+ " is interface? " + cc.isInterface());
    	}
    
    	public static void main(String[] args) 
    			throws ClassNotFoundException, InstantiationException, IllegalAccessException 
    	{
    		Class k = Toy.class;
    		printInfo(k);
    	}
    }
    I get the output:

    Class name: myPackage1.Toy is interface? false

    But isn't this class supposed to be not initiated ?

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Initialization of Classes ?

    I will answer my own question:

    Java Code:
    class Toy
    {
    	Toy()
    	{
    		System.out.println("Initialized!");
    	}
    	
    	static
    	{
    		System.out.println("Hello!");
    	}
    }
    Java Code:
    		Class k = Toy.class;
    		printInfo(k);
    		
    		
    		Class m = Class.forName("myPackage1.Toy");

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Initialization of Classes ?

    My only comment is:

    The constructor is not called, when the class is initialized. The output is:
    Class name: myPackage1.Toy is interface? false
    Hello!


    So it seems a constructor is called not when the class is initialized, but when an instance of that class is created.

    Initializing a class only calls the statements in the static{} block.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Initialization of Classes ?

    I would expect the Class object to be initialized when getName() is called.

    Note that loading the class and initializing it are two separate steps.

    The purpose of a constructor is to create an instance of the class, so it's no surprise that it doesn't run when the class is loaded or initialized.

    More here: Execution

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Initialization of Classes ?

    What if I have an instance variable in my class like int age and in my static block if I have something like this.age = 4;

    Then what happens ?

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Initialization of Classes ?

    Answering my own question:
    You can't use "this" in a static block.

Similar Threads

  1. Initialization
    By Sven in forum New To Java
    Replies: 4
    Last Post: 12-09-2008, 06:29 AM
  2. Initialization Error
    By Always.Smile in forum New To Java
    Replies: 7
    Last Post: 10-12-2008, 03:04 PM
  3. variable initialization
    By rqal.10 in forum New To Java
    Replies: 5
    Last Post: 06-15-2008, 07:39 AM
  4. 2D Array Initialization
    By M77 in forum Advanced Java
    Replies: 3
    Last Post: 06-04-2008, 03:12 PM
  5. initialization value problem
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 11:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •