Solved

Avoiding static

Posted on 2002-06-11
4
180 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
factorial example 4 50
Way to decrease size of apk file 9 88
jdbc error in jsp application 20 64
Selenium docs api java index 3 48
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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

821 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