Home java What is the unit static?

What is the unit static?




Where in real life can be used such a unit in programming? Why, then, the designer? Yes, I understand that the constructor will be called each time you create an instance of the class, and Static block once. But at what point will pass initialization Static block and where he actually used?

Answer 1, Authority 100%

Static blocks are called in the class initialization time (as the ClassLoader will ship it in MetaSpace, more details in the jLS ), and can be used for initsalizatsii static variables. Why is called every time the initialization of static variables in the constructor, if you can do it once, so do not waste resources on re-initialization?

Answer 2, authority 69%

I think you have a slight misunderstanding of the difference between the static and instance class fields.

The fields with the modifier static can be used even if it was not created more than one instance of a class (no subject) – and they are common to all instances of the class


static methods, respectively, can only interact with the static fields.

In the constructor, you can assign values ​​to the variables of the instance (object). For each instance is allocated a separate memory in which the stored instance variables, and they are different for each instance.

Also static initialize the unit if it is not Static initialization block – it runs before Designer, for example:

public class Test {
  static {
    System.out.println ( "Static");
    System.out.println ( "Non-static block");
  public Test () {
    System.out.println ( "Constructor");

Answer 3, authority 62%

Interestingly, I just used the last static block. I do not use it often, but here is a simplified example of my code to use it:

public class MovieData {
  public static List & lt; String & gt; titles = new ArrayList & lt; & gt; (),
    directors = new ArrayList & lt; & gt; ();
  static {
   add ( "Jurassic Park", "Steven Spielberg");
   add ( "The Dark Night", "Christopher Nolan");
   add ( "Titanic", "James Cameron");
  private static void add (String title, String director) {
   titles.add (title);
   directors.add (director);

As @zzashpaupat noted in its response, blocks static are invoked when initializing the class.

Answer 4, authority 54%

static block is executed the first time a class.
And it needed to initialize static fields, such as:

class Cls {
  static OtherCls someField;
  static String [] list = new String [3];
  static {
    someField = new OtherCls ();
    for (int i = 0; i & lt; list.length; i ++) {
      list [i] = Integer.toString (i);

Thus, OtherCls will intants-en, and the array will be filled before the first reference to the class Cls

Answer 5, authority 15%

This block – the block that will be executed once when the class is initialized (namely the class, not the instance). Actually, for this and needs.

I recently answering the question advised to maintain a set of methods in the dictionary. This could be done in a static unit, to be established once the completed dictionary. By the way, in C #, this could be done through more and collection initializer, but in Java such like are not.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions