?
Solved

Avoiding static

Posted on 2002-06-11
4
Medium Priority
?
184 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 200 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 200 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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