Results 1 to 4 of 4
  1. #1
    bolduc4 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default Global variable not accessible

    Hi,
    I'd like to declare a global variable in Main (or main), which is accessible from toto, in this code:
    Java Code:
    package pkg_main;
    [COLOR="Blue"]//here 1[/COLOR]
    public class Main{
    [COLOR="Blue"]  //here 2[/COLOR]
      public static void main(String[] args) throws Exception{
    [COLOR="Blue"]    //here 3[/COLOR]
        final IClient client=ClientFactory.getDefaultInstance();
        client.startToto(new [COLOR="Blue"]toto()[/COLOR]);
      }
    }
    I tried public int toto_age at the places //here without success.
    An idea ? Thk

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    There are no "global" variables in Java.

    Put a public variable in "here 1" then, if it is not static, access it through an instance of Main, if it is static then access it through the Main class i.e.
    Java Code:
    // non-static
    Main m = new Main();
    ... m.whatever ...
    // static
    ... Main.whatever ...
    Edit: The real question, however, is why you think you need it.

  3. #3
    bolduc4 is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    Thank you,
    I was wrong when calling the variable in toto class (forgetting "Main."whatever).
    I understand now the concept of class variable/instance variable.
    I've put the declaration at "here 2", not "here 1".
    --
    Why I need it: that's a parameter value. I can't modify the code of toto class to pass the value as an argument (that's proprietary and complex code).

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by bolduc4 View Post
    Thank you,
    I was wrong when calling the variable in toto class (forgetting "Main."whatever).
    I understand now the concept of class variable/instance variable.
    I've put the declaration at "here 2", not "here 1".
    Oops, yeah, that's what I meant. ;-)
    Why I need it: that's a parameter value. I can't modify the code of toto class to pass the value as an argument (that's proprietary and complex code).
    Uhm, I'm soory, but I no longer follow. What code uses that cariable then, if it's not meant to be used by "toto"? And what difference would it have made to forget "Main." when you can't modify the code that is using it (i.e. you never would have had to use "Main." in the first place)?

    Aach, nevermind, I foresee a branching that will probably dissolve into a long convulted conversion where nobody really knows what the other means, so just forget about it.

Similar Threads

  1. Replies: 7
    Last Post: 02-19-2010, 12:03 PM
  2. Replies: 1
    Last Post: 06-08-2009, 08:27 AM
  3. How to undefine a Global Variable in JAVA?
    By ajaykushwaha in forum New To Java
    Replies: 13
    Last Post: 11-19-2008, 11:53 PM
  4. Replies: 13
    Last Post: 08-23-2008, 10:09 PM
  5. Replies: 1
    Last Post: 08-06-2007, 03:17 AM

Posting Permissions

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