aspose file tools*
The moose likes Java in General and the fly likes Instance and Static initializers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Instance and Static initializers" Watch "Instance and Static initializers" New topic
Author

Instance and Static initializers

Caesar Dutta
Ranch Hand

Joined: Sep 18, 2004
Posts: 30
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

Joined: Feb 25, 2003
Posts: 51
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.


Alain Boucher<br />Spare-Brain Consultants Inc.<br />SCJ2P,SCWCD,
Caesar Dutta
Ranch Hand

Joined: Sep 18, 2004
Posts: 30
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Sep 18, 2004
Posts: 30
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

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Sep 18, 2004
Posts: 30
I would like to thank everybody for their nice explanation and taking the
time.

Warm regards,

Caesar
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Instance initializers allow for initialization of an object of an anonymous inner class--since anonymous inner class definitions cannot have constructors.

-j-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance and Static initializers
 
Similar Threads
annonymous class
static initializers
variable initializers vs instance initializers
terminology question
Code within a curly braces