Variables visible to all classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stack
    New Member
    • Sep 2007
    • 40

    Variables visible to all classes

    Hi,
    I wrote these 3 classes to explain my question:

    Code:
    public class Set {
    
        private BigInteger n;
        Random ran = new Random();
        
        public void setN(){
            n = new BigInteger(5, ran);
        }
        public BigInteger getN(){
            return n;
        }
    }
    
    public class Use {
        
        Set set1 = new Set();
         
        public void useN(){
            int nLength = set1.getN().toString().length();
        }
    }
    
    public class Test {
    
         public static void main(String[] args) {
           
             Set set1 = new Set();
             Use use1 = new Use();
             
             set1.setN();
             use1.useN();
        }
    }
    What I want to do is to use the Test class to set the value of the variable n from the Set class, and then use its value from the Use class. If I run this I get an error because the Use class see the value of n as null. How could I use the value of n in all my classes once it's set?

    Thank you very much
    stack
    Last edited by pbmods; Jan 13 '09, 12:23 AM. Reason: Added CODE tags.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    use1 contains a different set from set1.

    I think what you want to do is to make Set or the BigInt inside it a static variable. If you change the declaration to

    private static BigInteger n;

    it would probably work.
    ========
    Other possible solutions: Have a constructor in the Use class which takes a Set object.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      In your main method you create a Set and a Use. The Use has another Set in it. You call setN on the Set that you created in main not on the one that is in Use. But you try to use the n that belongs to the Set that is inside the Use object so it's value is never initialized.

      Don't create that other Set object in main. You already have a Set when you create a Use. All you need to do is to make that Set (inside the use) be available for use in your main.

      Comment

      • stack
        New Member
        • Sep 2007
        • 40

        #4
        Jkmyoung and r035198x thank you both for your help.

        r035198x, I did what you said and it worked. However, in order to make the Set (inside the Use) available in my main, I had to declare it static and therefore access it with the class name from main. If it's not static, how can I make it available in my main?

        Code:
        public class Use {
             static Set set1 = new Set();
          
             public void useN(){
                 int nLength = set1.getN().toString().length();
                 System.out.println("value of n from Use class is " + set1.getN());
             }
        }
        
        public class Test {
              public static void main(String[] args) {
           
                 Use use1 = new Use();
                
                 Use.set1.setN();
                 use1.useN();
                
                 System.out.println("Value of n from main is "+ Use.set1.getN());
              }
        }

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by stack
          Jkmyoung and r035198x thank you both for your help.

          r035198x, I did what you said and it worked. However, in order to make the Set (inside the Use) available in my main, I had to declare it static and therefore access it with the class name from main. If it's not static, how can I make it available in my main?
          If a variable (or method) isn't static it belongs to an object, an instantiation of that particular class. If there is no such instance there is no variable/method. So, you have to have access to an instantiation of that class in order to get access to your variable. People sometimes (ab)use the Singleton pattern for those purposes.

          If you want to have something globally available there's usually something rotten in your design; think twice before you want to implement such stuff.

          kind regards,

          Jos

          Comment

          • stack
            New Member
            • Sep 2007
            • 40

            #6
            If you want to have something globally available there's usually something rotten in your design
            True. I changed my design and everything makes sense now. Thank you for your input!

            Comment

            Working...