what do you mean by static block in java?

0 votes
Coming from C++, have no idea about static block? Please help!
Jun 18, 2018 in Java by Akrati
• 960 points
130 views

1 answer to this question.

0 votes

Static block is used for initializing the static variables.This block gets executed when the class is loaded in the memory. A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.

Example:

Here both the static variables were initialized before we accessed them in the main method.

class Example{
   static int n;
   static String str;
   static{
      n = 97;
      str = "Static keyword in Java";
   }
   public static void main(String args[])
   {
      System.out.println("Value of num: "+n);
      System.out.println("Value of mystr: "+str);
   }
}
answered Jun 18, 2018 by scarlett
• 1,280 points

Related Questions In Java

0 votes
1 answer

What do you mean by aggregation in Java?

Aggregation is a specialized form of Association ...READ MORE

answered Feb 17 in Java by Frankie
• 9,810 points
61 views
0 votes
1 answer

What do you mean by Socket Programming?

Socket programming is a way of connecting two ...READ MORE

answered Jul 14 in Java by Frankie
• 9,810 points
35 views
0 votes
1 answer

What do we understand from string pool in java?

This prints true (even though we don't use equals method: correct ...READ MORE

answered Apr 30, 2018 in Java by Daisy
• 8,050 points
43 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14 in Java by Sirajul
• 37,480 points
111 views
0 votes
1 answer

Interface variable are static and final by default. Why?

Interface variables are static because Java interfaces ...READ MORE

answered Jul 4, 2018 in Java by Daisy
• 8,050 points
2,035 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 10,222 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,110 points
2,288 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
5,947 views
0 votes
1 answer

What do you mean by object serialization ?

To serialize an object means to convert its state ...READ MORE

answered Jun 14, 2018 in Java by scarlett
• 1,280 points
82 views
0 votes
3 answers

What is ^ operator in java?

It's bitwise XOR, Java does not have ...READ MORE

answered Oct 31, 2018 in Java by Sushmita
• 6,880 points
38 views