Reputation: 335
I have the following class, listed below with static field of arrays. Could you please tell/explain how can I create constructor to initialize all of the arrays and pass it to the public static void BookInfo method. Any help is greatly appreciated!
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();
}
}
}
Upvotes: 4
Views: 8322
Reputation:
What you want to do is a use a static
block. static
data is initialized before any other code is run. The constructor
is only called when an object instantiated and they are called every time an object of that type is instantiated as well, static
is initialized once when the application is run.
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
}
It is also a good idea to make your static
variables final
as well.
Upvotes: 7
Reputation: 213261
Static variables are loaded in memory at the time of class loading. And at that time only they are initialized
. You don't need to initialize them in constructor.
You can however use static initializer block
to initialize them.
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
block is executed once during the class loading time. It takes care of intializing your static variables of class.
Moreover, static variables are shared amongst all instances of the class. So, any changes you make to them through one instance, will be reflected in all the instances. So, it doesn't make sense to have them initialized in a constructor.
Upvotes: 2
Reputation: 236014
It's possible to initializae the array in the declaration or in a static block, no need to use a constructor for this. For example:
private static String[] array = {"a", "b", "c"};
Or like this:
private static String[] array;
static {
array = new String[] {"a", "b", "c"};
}
In fact, initializing it in a constructor might be an error, a static member should not be changed each time a new instance is created by calling the constructor.
Upvotes: 2