Search This Blog

12 March, 2011

A Java program using static variables, methods and blocks.

Demonstrate  
    
                   "static variables, methods, and blocks".                      
 





class UseStatic
{ 
static int a = 3; 
static int b; 
static void meth(int x)
{ 
System.out.println("x = " + x); 
System.out.println("a = " + a); 
System.out.println("b = " + b); 
} 
static
 { 
System.out.println("Static block initialized."); 
b = a * 4; 
} 
public static void main(String args[])
 { 
meth(42); 
} 
}
---------------------------------x----------------------

class StaticDemo { 
static int a = 42; 
static int b = 99; 
static void callme() { 
System.out.println("a = " + a); 
} 
} 
class StaticByName { 
public static void main(String args[]) { 
StaticDemo.callme(); 
System.out.println("b = " + StaticDemo.b); 
} 
} 

Here is the output of this program: 
a = 42 
b = 99

---------------------------------------------------------------------------------------------------


No comments:

Post a Comment

Meetme@