3 Replies Latest reply: Sep 29, 2006 1:58 AM by 807598 RSS

    static class variable doesn't store ?

    807598
      Hi,
      I'm a beginner in java.

      I defined a static class variable (nodes) in th following code :
      public class MySOMapp extends javax.swing.JFrame {
      
           private static final long serialVersionUID = 1L;
           private static SOMTrainer trainer;
           private static SOMLattice lattice;
           private static SOMNode nodes; 
           private static Hashtable nwordf;  
      
           public MySOMapp(String args[]) {
                
                SOMNode nodes = new SOMNode();          //.....
      But a method of this class, nodes comes null. methos definition :
      private void makesame() {
      I don't understand why the nodes is null in makesame method which is in the same class with nodes ?
      thanks,
      Yigit
        • 1. Re: static class variable doesn't store ?
          807598
          Because in the constructor you also declared a local variable with the same name as your static field. Get rid of the local declaration:
              public MySOMapp(String args[]) {
                  nodes = new SOMNode();
          Also, why are you using only static fields? This is probably not a good idea.

          Regards
          • 2. Re: static class variable doesn't store ?
            807598
            thankyou. I solved the problem. But why static fields are not good idea ? How can i get rid of these...
            • 3. Re: static class variable doesn't store ?
              807598
              thankyou. I solved the problem. But why static fields
              are not good idea ? How can i get rid of these...
              Remove the static word :) Seriously, try to develop a technique to get swiftly out of the static main method. Like this:
              class MyClass {
              
                  MyClass() {
                      // Your code here instead of in main method.
                  }
              
                  public static void main(String[] args) {
                      new MyClass();
                  }
              }