Solved

Avoiding static

Posted on 2002-06-11
4
182 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
[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
  • 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

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!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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