Results 1 to 4 of 4
Thread: Global variable not accessible
- 05-09-2010, 10:23 PM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
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:
I tried public int toto_age at the places //here without success.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]); } }
An idea ? Thk
- 05-09-2010, 11:23 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
Edit: The real question, however, is why you think you need it.Java Code:// non-static Main m = new Main(); ... m.whatever ... // static ... Main.whatever ...
- 05-10-2010, 12:45 AM #3
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
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).
- 05-10-2010, 07:51 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Oops, yeah, that's what I meant. ;-)
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)?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).
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
-
Access restriction: Class is not accessible due to restriction on required
By kiranblighty in forum EclipseReplies: 7Last Post: 02-19-2010, 11:03 AM -
[error] No enclosing instance of type RobotTest is accessible
By jon80 in forum New To JavaReplies: 1Last Post: 06-08-2009, 07:27 AM -
How to undefine a Global Variable in JAVA?
By ajaykushwaha in forum New To JavaReplies: 13Last Post: 11-19-2008, 10:53 PM -
Making variables in a class accessible to all once changed
By int80 in forum New To JavaReplies: 13Last Post: 08-23-2008, 09:09 PM -
How would I declare the variable numbers as global?
By barney in forum New To JavaReplies: 1Last Post: 08-06-2007, 02:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks