GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 358 of 507    Print  
How to call a static block in one class from another class?. And is it possible to call a free floating block in one class from another class?. How?

  
Total Answers and Comments: 6 Last Update: June 11, 2008     Asked by: gothlururaaja 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: NavaNani
 
Non static blocks & Static blocks are executed one by one by the order of their defintion before any constructor is being executed.

Static blocks can also be invoked without creating instance of the Class(without actually calling constructors) by simply loading the Class.

Class.forName("ClassThatContainsStaticBlock");



Above answer was rated as good by the following members:
talktoatish
January 28, 2007 02:49:02   
alen strange        

RE: How to call a static block in one class from anoth...
If the scope allows we can call static class members by ClassName.MemberName because static member are fields of class instead of objects.
 
Is this answer useful? Yes | No
January 30, 2007 05:40:55   
aashish_bajpai Member Since: January 2007   Contribution: 1    

RE: How to call a static block in one class from anoth...
Both the class should be defined in the same package.and static block as public
 
Is this answer useful? Yes | No
May 18, 2007 06:49:50   
Vibs        

RE: How to call a static block in one class from anoth...
A static block of one class wll be called by another class when we try to instantiated the first class(the one having static block).
for e.g.

public class Test

{

int i ;static Test tobj null;

public Test ()

{

System.out.println("Constructor");

}

static

{

System.out.println("static");

}

}

public final class SubTest {

public SubTest()

{}

public static void main(String a[])

{

Test t new Test();//Static block of Test class will be called here

}

}


 
Is this answer useful? Yes | No
August 10, 2007 10:37:11   
sujatham Member Since: January 2006   Contribution: 142    

RE: How to call a static block in one class from anoth...
No.It is not possible.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
April 06, 2008 21:28:08   
NavaNani Member Since: April 2008   Contribution: 1    

RE: How to call a static block in one class from another class?. And is it possible to call a free floating block in one class from another class?. How?
Non static blocks & Static blocks are executed one by one by the order of their defintion before any constructor is being executed.

Static blocks can also be invoked without creating instance of the Class(without actually calling constructors) by simply loading the Class.

Class.forName("ClassThatContainsStaticBlock");


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 11, 2008 10:22:57   
vinaymudgil007 Member Since: May 2008   Contribution: 43    

RE: How to call a static block in one class from another class?. And is it possible to call a free floating block in one class from another class?. How?
static block gets executed whenever is class is loaded by the JVM
so what you can do is.....

write is class having static block in it ---> compile it --> write another class (ensure that first class is accessible to the second class) and instattiate first class in it ----> your static block will get executed



vinny

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape