Global variables in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    Global variables in Java

    Hi,
    Why there are no global variables in Java?
    please anyone brief it out..

    -Thanks & Regards,
    Hamsa
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by gaya3
    Hi,
    Why there are no global variables in Java?
    please anyone brief it out..

    -Thanks & Regards,
    Hamsa
    String s = new String("chupaka bras");

    if that initialization are inside a class but outside a method? is it possible?
    How do you call the s?

    Comment

    • gaya3
      New Member
      • Aug 2007
      • 184

      #3
      Originally posted by sukatoa
      String s = new String("chupaka bras");

      if that initialization are inside a class but outside a method? is it possible?
      How do you call the s?

      I think instance variable;correc t me if I'm wrong..

      -Thanks & Regards,
      Hamsa

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by gaya3
        I think instance variable;correc t me if I'm wrong..

        -Thanks & Regards,
        Hamsa
        have lots of terms,

        you may read this if you want.

        sukatoa

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          If you really, absolutely, positively, definitely, surely, urgently need global variables
          you can have them in Java:

          [code=java]
          public class Globals {

          // all your 'globals' here:
          static int foo;
          static double bar;
          static String baz;
          }
          [/code]

          Because all of the variables are static and public you can access/modify them
          in any other class and method:

          [code=java]
          class AnotherClass {
          final static double pi= Global.bar;
          final int buz= Global.foo;
          String myBaz;
          public AnotherClass(St ring baz) { Global.baz= myBaz= baz; }
          }
          [/code]

          I consider coding like this as Basic and Fortran coding *yeach*

          kind regards.

          Jos

          Comment

          Working...