Solved

Java static array initialize....

Posted on 2012-04-02
10
788 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 85
servlet requestdispatcher include and forward differences 1 32
egit plugin on eclipse 8 64
how to install java on RHEL image on EC2 4 28
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

803 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