saving . . . saved difference has been deleted. difference has been hidden .
difference
Title
Question
What is difference between static and non-static block?

Java Non-static-block 00-01 min 0-10 sec 25-03-14, 3:36 p.m. vedpatial

Answers:

Non-static block is executed only when a class object is created.
It is executed each time an object is created, and can also access class variables.        

Static block is executed when the class is loaded by the Java Virtual Machine(i.e. when the code is executed).
It is executed once, and can access only the static variables.
25-03-14, 5:25 p.m. pratham920


Hi,

To access astatic method we don't need any object.
A non-static method is always called using the object of class.

We cannot access a non-static variable inside static method but static variables can be accessed in a non-static method.

Example of a static method:

class Demo
{
   public static void display(String str1, String str2)
   {
       System.out.println("First String is: "+str1);
       System.out.println("Second String is: "+str2);
   }
   public static void main(String agrs[])
   {
      display("ice", "cream");
   }
}

The output will be:
First String is: ice
Second String is: cream

Here, We didn't used an object to call the function. It can be directly called or you can use class name also(class.static_method)

==============================================================================

A non-static method is always called by using object of the class.

Example of a non-static method:

import java.io.*;
class demo_nonstatic
{
   public void display(String str1, String str2)
   {
       System.out.println("First String is: "+str1);
       System.out.println("Second String is: "+str2);
   }
   public static void main(String agrs[])
   {
       demo_nonstatic obj=new demo_nonstatic();
       obj.display("ice", "cream");
   }
}

Here, you can see the method is called using object.




25-03-14, 5:45 p.m. Ashwini


Static :gets executed only when the program runs

Dynamic: gets executed only when the class in the program gets executed , Dynamic is also known as Non-static
06-12-21, 8:53 p.m. Harish1605


Log-in to answer to this question.