Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java static array initialize....

Posted on 2012-04-02
10
Medium Priority
?
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 2000 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 36

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 36

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 36

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 2000 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 36

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

636 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