?
Solved

Java static array initialize....

Posted on 2012-04-02
10
Medium Priority
?
797 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

765 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