what is the difference between statis block and static
variable

Answer Posted / manikandan [ gtec,vellore ]

static variables hold only one value and static block is a
set of code. static variables and static methods are loaded
before a main method.it loads one by one in top down approach.

consider this code:

class test
{
static{
System.out.println(i);//compile error bcas top down approach
}

static int i=1;
public static void main(String[]asd)
{

}
}

Is This Answer Correct ?    6 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is jit and its use?

585


Define iterator and methods in iterator?

547


In a container there are 5 components. I want to display the all the components names, how will you do that one?

606


When throws keyword is used?

584


What is byte code and why is it important to java’s use for internet programming?

594






What are the different conditional statements?

558


What is loop in java?

533


When a thread is executing synchronized methods , then is it possible to execute other synchronized methods simultaneously by other threads?

569


Can we create a constructor in abstract class?

575


What restrictions are placed on method overriding in java programming?

543


How do you compare objects in java?

503


Break statement can be used as labels in java?

554


Is ++ operator thread-safe in java?

636


Can we create an object if a class doesn't have any constructor ( not even the default provided by constructor ) ?

554


How can I become a good programmer?

490