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
926 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,290 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 18, 2019 in Java by Frankie
• 9,830 points
812 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, 2019 in Java by Frankie
• 9,830 points
1,161 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,140 points
730 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, 2019 in Java by Sirajul
• 59,230 points
3,496 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,140 points
13,530 views
0 votes
0 answers

Can I override and overload static methods in Java?

My questions are: Why can't static methods be ...READ MORE

May 15, 2022 in Java by Kichu
• 19,040 points
521 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 81,388 views
+1 vote
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,500 points
8,407 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,290 points
1,048 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,920 points
1,136 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP