Results 1 to 14 of 14
  1. #1
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default Making variables in a class accessible to all once changed

    I have a class that just holds values that are updated as the program receives user input. But I want this classes variables once updated to accessible to all the other classes. I can't for the life of me remember how to do that.

    class1 {
    variable1;
    variable2;
    }

    class2 {
    class1 class = new class1();
    class.variable1 = "Java";
    }

    class3 {
    I want to be able to change the value of class.variable1 here, and have that value remain in variable1 and be accessible to all.
    }


    thanks in advance.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    I think you need make variable1 as a class variable, so you'll need to use the static keyword to make variable1 a class variable.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Also if you make the variable public, other classes will be able to see/get it.

    static doesn't make a variable a class variable. static means that the variable has only one existence no matter how many instances of the class there are. If there are multiple instances of a class, a static variable would be shared by them all. If any instance changed its value, all other instances would see the change.
    A class variable would exist in each class, one per class.

    Make a small test program and try the different combinations to see which solves your problem.
    Last edited by Norm; 08-23-2008 at 04:00 AM.

  4. #4
    udayadas's Avatar
    udayadas is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    Yea, when you declare like

    public static int variable;

    you can access it by "Class1.variable" from anywhere.

    if you do it like...

    public int variable ;

    then you can access it by creating object of Class1 from any where.

    Class1 cc=new Class1();
    cc.variable=100;

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by udayadas View Post
    public static int variable;
    you can access it by "Class1.variable" from anywhere.
    True, but doing so is a sin. Maybe two sins.

    1) It exposes all the details to the world. You can not, for example, change it from an int to a float without breaking the world.
    2) global variables radically increase coupling and cohesion. The point of OO is to get software reuse by decreasing coupling and cohesion.

  6. #6
    udayadas's Avatar
    udayadas is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default Coupling

    Quote Originally Posted by int80 View Post
    Making variables in a class accessible to all once changed
    this is what int80 wants to achive

    As far as Coupling part is concerned....You cant really live without it.At best you can reduce it.Make that member private and provide setter and getter for it.Any other idea??

  7. #7
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    Thanks alot guys! Once you use static, you obviously don't have create new instances of it either, and they become more like C structs and inter-useable (if you know what I mean), just what I wanted!

    It's all starting (slowly) to fall into place now.

    Also, I have one last question:

    If you are in a method within a method like this:

    Java Code:
    class xxx {
    
     public void MethodCall(){
    
          public void methodcallChecker(){
    >>            ///something is found to be true here, so restart methodcall()
         }
     }
    }
    I want to know if it's possible to restart "MethodCall()" from within methodcallChecker(), so MethodCall starts again. It's been puzzling me for ages; I tried using "this", but that doesn't work.

    Again, thanks in advance.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    does your "method within a method" code even compile? Please try that and get back to us with your result.

  9. #9
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    Ok, I get what you're saying...

    Java Code:
    class xxx {
    
     public void MethodCall(){
    
      class xxxx {
          public void methodcallChecker(){
    >>            ///something is found to be true here, so restart methodcall()
         }
      }
     }
    }
    I mean like that.

  10. #10
    udayadas's Avatar
    udayadas is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    He..He.. I belive....java does not allow this

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    java does not allow this
    Not allow what?
    The test would be to see if it compiles?
    so restart methodcall()
    What does that mean? Would the Checker method throw an exception that would bubble up to the caller of the method MethodCall?

  12. #12
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    I'm pretty confused to tell you the truth.

    Java Code:
    class xxx {
     public void MethodCall(){
    
      final class xxxx {
          public void methodcallChecker(){
    >>            ///something is found to be true here, so restart methodcall()
         }
      }
     }
    }
    MethodCall() is called from the main() class, so maybe I could call it in a for( ;; ) loop, and return back to the main class once methodcallChecker has found something to be true. Then it'll keep getting called anew?

    It's weird, I've actually managed to write a reporting engine for a MySQL database, but just get stuck on these weird little problems. I've got gaping holes in my Java knowledge that seriously hinder me. I really need to get reading.

  13. #13
    udayadas's Avatar
    udayadas is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    Java does not allow nested methods.

    On the other hand method local inner classes are allowed

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    You need to explain how/why the various methods are being called.
    Get a design before coding is always a good rule.

Similar Threads

  1. why my url string is changed in runtime ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 08-13-2008, 10:46 PM
  2. Help with making this algorithm better
    By RLRExtra in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 05:11 PM
  3. Why the panel text changed?
    By ottawalyli in forum AWT / Swing
    Replies: 1
    Last Post: 12-17-2007, 06:56 AM
  4. Why the panel text changed?
    By ottawalyli in forum SWT / JFace
    Replies: 0
    Last Post: 12-16-2007, 05:16 PM
  5. Strings are immutable yet they can be changed ?
    By anjanesh in forum New To Java
    Replies: 4
    Last Post: 05-19-2007, 04:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •