Example 2 : forName()

We can execute static block ( without using object ) by using forName().

smo_main.java

public class smo_main {

public static void main(String[] args)throws Exception {
int x=5;
int y=16;
smo_1 obj1=new smo_1();
System.out.println(obj1.my_add(x,y));

Class.forName("smo_1"); // smo_1 class static block 

}
}

smo_1.java

public class smo_1 {
static {
	System.out.println("I am static"); 
}
	
int my_add(int x,int y)
{
	return x+y;		
}
}
Output
I am static
21
Class object method

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer