Solved

Java static array initialize....

Posted on 2012-04-02
10
791 Views
Last Modified: 2012-08-13
I tried this in one of my classes, is this possible in java? (In other words, how do you create a static list of objects without having to create it at runtime?

protected class widget {
   int id;
   String name;
   String value;
}

private static widget[] widgetlist {
 {0, "statue", "$5.95"},
 {1, "vase", "$10.50"},
 {2, "desk", "see master list"}
};
0
Comment
Question by:bganoush
  • 5
  • 4
10 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37799357
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 500 total points
ID: 37799488
add a constructor or get/set ers to widget class,

protected class Widget {
   int id;
   String name;
   String value;

   public Widget( int id, String name, String value ){
     this.id = id ;
     this.name = name ;
     this.value = value ;
   }
}

and then define the static array,

private static Widget[] arr = new Widget[ 3 ] ;

static{
  arr[0] = new Widget( 0, "statue", "$5.95" ) ;
  arr[1] = new Widget( 1, "vase", "$10.50" ) ;
  arr[2] = new Widget( 2, "desk", "see master list" ) ;
}
0
 
LVL 35

Expert Comment

by:mccarl
ID: 37799638
Also, once you have the constructor as ksivananth mentioned, you can initialise in this way...

private static widget[] widgetlist {
 new Widget(0, "statue", "$5.95"),
 new Widget(1, "vase", "$10.50"),
 new Widget(2, "desk", "see master list")
};
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:ksivananth
ID: 37799642
>>you can initialise in this way...

the problem with this is, each time you call this method, a new array created and initialized which you ultimately wouldn't want!
0
 
LVL 35

Expert Comment

by:mccarl
ID: 37799653
> each time you call this method

What method are you referring to?
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 37799657
widgetlist()
0
 
LVL 35

Expert Comment

by:mccarl
ID: 37799677
???

widgetlist is a private static member, not a method!? There are no methods in what the OP posted!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 37799696
>>widgetlist is a private static member, not a method

private static member with initialization can't be this way, it should be as I posted earlier,

private static Widget[] arr = new Widget[ 3 ] ;

static{
  arr[0] = new Widget( 0, "statue", "$5.95" ) ;
  arr[1] = new Widget( 1, "vase", "$10.50" ) ;
  arr[2] = new Widget( 2, "desk", "see master list" ) ;
}
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 total points
ID: 37799701
or this way

private static widget[] widgetlist = {
 new Widget(0, "statue", "$5.95"),
 new Widget(1, "vase", "$10.50"),
 new Widget(2, "desk", "see master list")
};
0
 
LVL 35

Expert Comment

by:mccarl
ID: 37799726
Ok, so I forgot the equals sign! :)
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
map related example 6 59
fibonacci ten numbers 4 54
how to see all occupied ports on windows 10 laptop 15 82
ejb entity bean example 2 31
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question