• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializers in program.

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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
Ragards
Prasad.


[This message has been edited by Prasad Ballari (edited November 16, 2000).]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.
Example:
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 );
}
}
Output:
Static Initializer Executed
9
0
false
null
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic