Solved

Avoiding static

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

706 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

15 Experts available now in Live!

Get 1:1 Help Now