File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initializers in program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializers in program." Watch "Initializers in program." New topic

Initializers in program.

Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Can anybody help me, What is the difference between class variable initializer and instance variable initilizer.And how can i write each one of them.
(The basic concept what i know is that, class initializers are used to initialize class variables and instance initializers are used to initialize instance varibales.).
Can anybody help me.( with example).
Thanks in advance

[This message has been edited by Prasad Ballari (edited November 16, 2000).]
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
Static Initializer is initialised when the class is loaded and even before the main() method is executed. Static variables are also known as class variables.
Instance members (variables/methods) belong to every instance of a class.
class A
static // 1 ==> Static Initializer
System.out.println("Static Initializer Executed");
static int x=9; // 2 ==> class variable
int y; // 3 ==> Instance variable
boolean answer; // 4 ==> Instance variable
String s; // 5 ==> Instance variable
public static void main(String [] args)
A a = new A();
System.out.println(" x = " + A.x );
System.out.println(" y = " + a.y );
System.out.println(" answer = " + a.answer );
System.out.println(" String s = " + a.s );
Static Initializer Executed
From the above output,it is clear that static initializers are executed even before the main() method is executed.
The instance variables are initialised with the default values, i.e "0" for int, "false" for boolean, "null" for String/any Object.
The static variable "x=9" is accessed by its class name "A".
- Suresh Selvaraj

Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: Initializers in program.
It's not a secret anymore!