This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance and Static initializers

 
Caesar Dutta
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to know more about Instance and Static initializers.
And when I should use them ?

Can anybody kindly help me with these terms - "Loading a class" and
"then initializing a class" ? I am asking this as - can a class be loaded
but not initialized ? If so when does this happen.

Any explanation on this will be greatly appreciated.

Regards,

Caesar
 
Alain Boucher
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 .public class TestObject{
2 .
3 .int integerNumber;
4 .String sentense;
5 .
6 .public void myfunction(){
7 . String string1;
8 .
9 . string1 = "toto";
10. }
11.}

When you declare a object
TestObject obj1; //This is declaration

obj1 = new testObject(); //Instanciation

When the Object is created, the value of integerNumber (line 3) and sentense (line 4) are set to 0 and null

In the function at line 6 the declaration of object string1 is ok, but it's not null... because in a method object aer not automaticly instantiate... so you have to initialize it (line 9) to use it.
 
Caesar Dutta
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alain.

But what is meant by saying "Class is loaded". And under what
circumstances should I use initializers (Static /Instance).

An answer to these would be greatly appreciated.

Regards, Caesar
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class is loaded can be loaded manually -- Class.forName("com.package.Foo") -- as well as automatically when some other class that references it is loaded. For example, if you do

java com.package.Foo

The JVM starts up, the class Foo is loaded, and its main() method is executed.

The static initializer for the class is executed when the class is loaded. This can be useful for bootstrapping initialization of the class. This just means that the JVM executes the initialization automatically.

For example, say you want to create a singleton instance of the class when ti is loaded instead of waiting for the first call to getInstance(). You could load the class and call the initialization method yourself in code, or you could have the static initializer call it.

Instance initializers are executed before the constructor for each instance. In fact, they are copied into each constructor (before the constructor code) during compilation. One reason to use them then would be to be able to execute code before the constructor.

Recall that the first line of a constructor must be a call to the superclass constructor, or the compiler will generate a call to the default constructor.
 
Caesar Dutta
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First I should say that the explanation of Instance/Static initializer
was very helpful

Quote
The JVM starts up, the class Foo is loaded, and its main() method is executed.
End Quote

That means whenver the class gets loaded its main() method gets
executed. But a class may not have a main method. I am now confused.

Suppose C is a class

What is the difference bewteen
1) C c = new C() and
2) forName(C)

My understanding is 1) - Load and initialize and for 2) I do not know...

I have seen in JDBC that
try {
Class.forName(("oracle.jdbc.driver.OracleDriver");
}catch(Exception e) {
System.out.println("Error = " + e.getMessage());
}

Then I use DriverManager.getConnection()

I do not write
DriverManager d = Class.forName(("oracle.jdbc.driver.OracleDriver");
and then
d.getConnection(required parameters.)

All these things are confusing me.

Caesar
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means whenver the class gets loaded its main() method gets
executed. But a class may not have a main method. I am now confused.

Actually, only the class xxx named in the java xxx command has its main method autmatically executed.

Generally speaking, a class (and any not-yet-loaded parents) gets loaded the first time it is referenced in your program. Loading a class means reading its .class file, usually from the CLASSPATH, setting up the bytecode in memory, allocating static variables in memory, executing static initializers (both static initialization blocks and initializers in static variable declarations, and loading some internal jvm tables that constitute the "class object".

Instance initializers are executed just before the constructors each time a class instance (an object) is created.
[ October 07, 2004: Message edited by: Mike Gershman ]
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Caesar Dutta:
That means whenver the class gets loaded its main() method gets
executed.

No, as Mike clarified, that happens only in my example because the command that started the JVM told it to execute Foo's main() method -- not because the class got loaded.
Originally posted by Caesar Dutta:
What is the difference bewteen
1) C c = new C() and
2) forName(C)

Both will cause C to be loaded if it hasn't been already, but only (1) creates a new instance of C.

Your JDBC example is the most typical use of Class.forName(name). The generalization is that the class name cannot show up in code since the specific class will be determined at runtime, usually by a String read from a property file.

The code references a superclass or interface which the specific class will extend/implement, just as with java.sql.Driver and oracle.jdbc.driver.OracleDriver. DriverManager knows only about the former.

If you were writing a simple test program and didn't want to deal with property files, you could just as easily declare a variable of type OracleDriver directly instead of using Class.forName():
 
Caesar Dutta
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to thank everybody for their nice explanation and taking the
time.

Warm regards,

Caesar
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance initializers allow for initialization of an object of an anonymous inner class--since anonymous inner class definitions cannot have constructors.

-j-
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic