Class and object initialization
Learn how to prepare classes and objects for use in an executing program
By Jeff Friesen, JavaWorld.com, 11/02/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Page 2 of 9
Listing 2. ClassInitializationDemo2.java
// ClassInitializationDemo2.java
class ClassInitializationDemo2
{
static boolean b = true;
static byte by = 1;
static char c = 'A';
static double d = 1.2;
static float f = 3.4f;
static int i = 2;
static long l = 3;
static short s = 4;
static String st = "abc";
public static void main (String [] args)
{
System.out.println ("b = " + b);
System.out.println ("by = " + by);
System.out.println ("c = " + c);
System.out.println ("d = " + d);
System.out.println ("f = " + f);
System.out.println ("i = " + i);
System.out.println ("l = " + l);
System.out.println ("s = " + s);
System.out.println ("st = " + st);
}
}
In contrast to ClassInitializationDemo1, in ClassInitializationDemo2 a class field initializer explicitly assigns a nondefault value to each class field. Simply put, a class field initializer
consists of the assignment operator (=) and an expression that the JVM evaluates after a class loads and before any of that class's developer-specified methods
execute. The assignment operator assigns the expression's value to the associated class field. When run, ClassInitializationDemo2 produces the following output:
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download this article's source code and resource files
http://www.javaworld.com/javaworld/jw-11-2001/java101/jw-1102-java101.zip
- For a glossary specific to this article, homework, answers to the Java 101 Challenge, and more, see the Java 101 study guide that accompanies this article
http://www.javaworld.com/javaworld/jw-11-2001/jw-1102-java101guide.html
- The Java Language Specification, Second Edition, James Gosling, Bill Joy, Guy Steele, Gilad Bracha (Sun Microsystems, 2000)
http://www.javasoft.com/docs/books/jls/second_edition/html/j.title.doc.html
- The Java Tutorial
http://www.javasoft.com/tutorial/
- Sun's official Java language definition
http://java.sun.com/docs/overviews/java/java-overview-1.html
- Learn how to ensure that your objects are properly initialized at all times in "Designing Object Initialization," Bill Venners
(JavaWorld, March 1998)
http://www.javaworld.com/jw-03-1998/jw-03-techniques.html
- Review Jeff's previous column, "Object-Oriented Language Basics, Part 7" (JavaWorld, October 2001)
http://www.javaworld.com/javaworld/jw-10-2001/jw-1005-java101.html
- Go on to Jeff's next Java 101 lesson"Trash Talk, Part 1" (JavaWorld, December 2001)
http://www.javaworld.com/javaworld/jw-12-2001/jw-1207-java101.html
- Check out past Java 101 articles
http://www.javaworld.com/javaworld/topicalindex/jw-ti-java101.html
- For more articles on Core Java concepts, browse our Topical Index
http://www.javaworld.com/channel_content/jw-core-index.shtml
- Need some Java help? Visit our Java Beginner discussion
http://forums.idg.net/webx?50@@.ee6b804
- Java experts answer your toughest Java questions in JavaWorld's Java Q&A column
http://www.javaworld.com/javaworld/javaqa/javaqa-index.html
- For Tips 'N Tricks see
http://www.javaworld.com/javaworld/javatips/jw-javatips.index.html
- Sign up for JavaWorld's free weekly Core Java newsletter
http://www.idg.net/jw-subscribe
- You'll find a wealth of IT-related articles from our sister publications at IDG.net
Very helpful article, thanxBy Anonymous on September 28, 2009, 7:56 pmThis question is risen by young curious java students. But there are low chances to describe without the deep concept understanding and seeing the whole picture....
Reply | Read entire comment
I'm always curious about it...By Anonymous on September 3, 2009, 2:59 amI'm always curious about it. You gave me a highway express. Thanks a lot.
Reply | Read entire comment
Very usefulBy Anonymous on May 3, 2009, 11:30 amThank you
Reply | Read entire comment
View all comments