Solved

Avoiding static

Posted on 2002-06-11
4
178 Views
Last Modified: 2010-03-31

I have an array which is instantiated in one class, and referenced in a few others. If I declare
the array static everyting compiles and runs, but references to the array give me erratic
performance.

Removing the static declaration results in the following compiler error:

"Can't make a static reference to nonstatic variable entryforms in class InstantiatingClass.
               method( InstantiatingClass.entryforms[ i ].someVariable, ... );"
                                                                                          ^
What adjustments do I make to the outside references in order to keep the array declaration non-static?


A little more detail: -----------------------------------------------------------------------
[code]
class InstantiatingClass extends MouseAdapter {

     static EntryForm[] entryforms;     //     This compiles but gives screwy results
     EntryForm[] entryforms;          //     References to this do not compile
     
     ...
     
     entryforms = new EntryForm[15];    
     ...
}


class ReferrringClass extends MouseAdapter {

...

EntryForm[] entryforms;
     
     public ReferrringClass( EntryForm[] entryforms ) {
                         
          this.entryforms = entryforms;

     }

          for( int j = 0; j < 15; j++ ) {
               if( e.getSource() == entryforms[j].submitButton ) {
                   
                    i = j;                                                  //     "i" is the INSTANCE of the form
                    break;
               }
          }
          ...
                         
          if ( entryforms[ i ].someVariable == "Something" ) {     // Results in null pointer errors at runtime          
                         
          if ( InstantiatingClass.entryforms[ i ].someVariable == "Something" ) {     // References like these result in compiler errors          
          ...
                             
          }

}
[/code]
Many thanks in advance.
0
Comment
Question by:javaQQ
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 50 total points
ID: 7070666
 If your variable is not static you cannot call it like:

 InstantiatingClass.entryforms[ i ].someVariable

  instead you will need to create a reference to the class and call it (if it is public) through the reference:

  InstantiatingClass ic = new InstantiatingClass();
  ic.entryforms[i].someVariable;

  Hope it helps.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 7071388
> if ( entryforms[ i ].someVariable == "Something" ) {
> // Results in null pointer errors at runtime          

Sounds like you haven't initialised entryForms correctly.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7072003
You can implement the singleton pattern, like this :

public final class ArrayClass {
  private static final ArrayClass arrayClassObject = null;
  EntryForm[] entryforms = new EntryForm[20];

  private ArrayClass() {
    //do some init...
  }

  public static final ArrayClass getArrayClassObject() {
    if(arrauClassObject == null)
      arrayClassObject = new ArrayClass();
    return(arrayClassObject);
  }
}


After that you should be able to use'it from any point like this :
ArrayClass.getArrayClassObject().entryForms;
or
ArrayClass.getArrayClassObject().entryForms[0];
ArrayClass.getArrayClassObject().entryForms[1];
ArrayClass.getArrayClassObject().entryForms[2];

0
 
LVL 35

Expert Comment

by:girionis
ID: 8834385
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- split points between girionis and objects@idg

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 video teaches viewers about errors in exception handling.

863 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

26 Experts available now in Live!

Get 1:1 Help Now