【问题标题】:How to use constructor for java static array initialization?如何使用构造函数进行 java 静态数组初始化?
【发布时间】:2012-10-09 01:07:53
【问题描述】:

我有以下类,下面列出了数组的静态字段。 您能否告诉/解释我如何创建构造函数来初始化所有数组并将其传递给公共静态 void BookInfo 方法。非常感谢任何帮助!

  public class BookInfo {

    // Global arrays accessible by all methods
public static String[] isbnInfo = {

        "978-0060014018",
        "978-0449221431",
        "978-0545132060",
        "978-0312474881",
        "978-0547745527"

      };


public static String[] bookTitleInfo = {

            "The Greatest Stories",
            "The Novel",
            "Smile",
            "The Bedford Introduction to Drama",
            "AWOL on the Appalachian Trail"

           };

public static String[] authorInfo = {

         "Rick Beyer",
         "James A. Michener",
         "Raina Telgemeier",
         "Lee A. Jacobus",
         "David Miller"

        };

public static String[] publisherInfo = {

            "HerperResource",
            "Fawcett",
            "Graphix",
            "Bedford St. Martins",
            "Mariner Books"

            };

public static String[] dateAddedInfo = {

            "05/18/2003", 
            "07/07/1992", 
            "02/01/2010", 
            "09/05/2008", 
            "11/01/2011"

            };

public static int[] qtyOnHandInfo = {7, 5, 10, 2, 8};

public static double[] wholesaleInfo = {12.91, 7.99, 6.09, 54.99, 10.17};

public static double[] retailInfo = {18.99, 3.84, 4.90, 88.30, 14.95};

public static void BookInfo() {

    System.out.println("             Serendipity Booksellers");
    System.out.println("                Book Information\n");       


    for(int i = 0; i < isbnInfo.length; i++){

        System.out.println("ISBN: " + isbnInfo[i]);
        System.out.println("Title: " + bookTitleInfo[i]);
        System.out.println("Author: " + authorInfo[i]);
        System.out.println("Publisher: " + publisherInfo[i]);
        System.out.println("Date Added: " + dateAddedInfo[i]);
        System.out.println("Quantity-On-Hand: " + qtyOnHandInfo[i]);
        System.out.println("Wholesale Cost: $ " + wholesaleInfo[i]);
        System.out.println("Retail Price: $ " + retailInfo[i]);
        System.out.println();

         }
            }
    }

【问题讨论】:

  • 你不应该在你的构造函数中初始化它们。如果你这样做了,每次你通过该构造函数创建一个实例时,那些静态数组将再次“初始化”。所以想一想,这是你想要的吗?

标签: java arrays oop static


【解决方案1】:

您想要做的是使用static 块。 static 数据在运行任何其他代码之前被初始化。 constructor 仅在对象实例化时调用,并且每次在实例化该类型的对象时调用它们,static 在应用程序运行时初始化一次。

public static final String[] ISBN_INFO; 

static 
{ 

    BookInfo.ISBN_INFO = {
    "978-0060014018",
    "978-0449221431",
    "978-0545132060",
    "978-0312474881",
    "978-0547745527" }; 

  // do the same thing with the other blocks

}

static 变量设置为final 也是一个好主意。

【讨论】:

  • 如何从 public static void BookInfo 方法和其他类访问?
【解决方案2】:

可以在声明或静态块中初始化数组,无需为此使用构造函数。例如:

private static String[] array = {"a", "b", "c"};

或者像这样:

private static String[] array;
static {
    array = new String[] {"a", "b", "c"};
}

实际上,在构造函数中初始化它可能是一个错误,不应在每次调用构造函数创建新实例时更改静态成员。

【讨论】:

    【解决方案3】:

    静态变量在类加载时加载到内存中。而当时只有他们是initialized。你不需要在构造函数中初始化它们。

    不过,您可以使用static initializer block 来初始化它们。

    private static int[] arr;
    static {
        // initialize your arrays here in static initializer block.
         arr = new int[5];
         arr[0] = 5; arr[1] = 10; ... so on
    }
    

    Static initializer 块在类加载期间执行一次。它负责初始化类的静态变量。

    此外,静态变量在类的所有实例之间共享。因此,您通过一个实例对它们所做的任何更改都将反映在所有实例中。因此,让它们在构造函数中初始化是没有意义的。

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多