what is the difference between static block and static
method
Answers were Sorted based on User's Feedback
Answer / aluru srinivasa rao
static block is executed first even before constructor is
invoked,static methods are invoked from a static method.
| Is This Answer Correct ? | 8 Yes | 1 No |
Answer / anu
static block is execute first after the remaining blocks will executes.
static metods can executes using classname.staicmethodname
| Is This Answer Correct ? | 4 Yes | 0 No |
Answer / chandu
write down compilation sequence order?
Static variable
Static block
Static method
?
| Is This Answer Correct ? | 5 Yes | 3 No |
Answer / vilas paskanti
@Maruti
Main Method is a Special method That is called by the JVM.
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / uday
static block is called when we create an object for that class,so it automatically excutes......
for static methods, we can call this methods by class names not by object....
| Is This Answer Correct ? | 2 Yes | 1 No |
Answer / mittan aich
static block:
It is the class level one time execution block.After the compilation of the program when we go for running by using the command java <class name> then only the corresponding class will loaded in to RAM and 1st of all the static block of that class will execute even it run before the main method run.
->We place the class level initialization logic here like initialization of static variable.
Ex:
class Alpha
{
static
{
System.out.println("Alpha:static");
}
public void show()
{
System.out.println("Alpha:Show()");
}
}
public class Bita
{
static
{
System.out.println("Bita:static");
}
public static void main(String args[])
{
Alpha a=new Alpha();
a.show();
}
}
OUTPUT:
Bita:static
Alpha:static
Alpha:Show()
.......................
STATIC METHOD:
static method is a method which can loaded into the RAM without any object only class name is enough.
->Actually what happen fnds??A function will execute whn ever it only loaded into the RAm through a object.But some times our requirement is like this ,we have to execute a function but at that time no object cant be created for that
class.at that moment static method is very necessary
(consider about main method of java).
->static method and static variable does not allocated memory in object.but it allocate memory in CONTEXT area of that class.
EX:
class Gamma
{
int count1;
static int count2;
static void show()
{
System.out.println("Gamma:show()");
count1++;//error bcz non static variable.
count2++;//ok
}
public static void main(String args[])
{
Gamma.show();
}
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / uday
maruthi we are calling main method....
without calling how it excutes........?
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / sukhvider
static block executes whenever the class loaded in the Java virtual machine that means object is before going to main Method.
| Is This Answer Correct ? | 1 Yes | 4 No |
Answer / hathwar
static block will execute when ever execution starts, it
means this block executes first of all, where as static
block have to call seperately and also it can call with out
object instance.
| Is This Answer Correct ? | 41 Yes | 51 No |
Why declare Main() method as a static in java ?
What is a consumer in java?
Why singleton pattern is better than creating singleton class with static instance?
How do you reverse a string in java without using string buffer?
What is serialVersionUID and what is its need?
What is arrays aslist in java?
How do you declare an array in java?
Why is multiple inheritance not supported in java?
What is the protocol is used in type4 driver?
Does it matter in what order catch statements for filenotfoundexception and ioexception are written?
What is the difference between throw and throws? What is the similarity between try and throw?
What are the skills required for core java?