Solved

Java static array initialize....

Posted on 2012-04-02
10
785 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now