Java-->Global Variables--> ???

Started by
11 comments, last by brunogmd 19 years, 10 months ago
hi I was wondering, how can I make a variable declared inside main function visible to all functions in Java ? (I dont want to take arguments) for example: public class example { public static void main (String arg[]) { double examp; function (); } public static void function (); { examp = 400.44; } } Thaaanks.
Bruno B
Advertisement
You cannot. A variable declared inside a function is visible only inside that function.
Is there ANY language where a variable defined in a function can be global?

Anyway, to make a ''global'' in Java, just make it public static.
And how can I make a variable visible to all functions ?
I cant !!! ???
there is no way ?
Bruno B
quote:Original post by pinacolada
Is there ANY language where a variable defined in a function can be global?


Lua defaults all variables to global, unless you specify
that they are "local".

Yes, this applies to variables defined in a function.


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Just define a public static class variable, then that variable is global. You can access that variable by CLASS_NAME.VARIABLE_NAME.
quote:Original post by brunogmd
And how can I make a variable visible to all functions ?
I cant !!! ???
there is no way ?


You can, just not how you asked. If you want to make a variable global to it's class, just declare it outside of any function, and initialize it inside one of your functions.

eg:

public class Test{	String var;   // this is visible to all functions of this class	public void test()	{	    var = new String("I'm using var");	}}


Other vars, like integers, don't need to be initialized with the new keyword first.


[edited by - botman2 on June 11, 2004 2:09:52 AM]
botman2 is right, except you can get away with just saying

var = "hello";

the compiler works it all out for you. Like when you add strings it actually uses a StringBuffer - but the complexity is hidden from you. String in Java is a bit of an odd-ball, every other complex object you have to use the new thing.

If you want to make it available to all classes you declare var as public.

If you want it to be shared among all instances of a class you declare it static.

If you want it shared between all instances of a class and available to other classes you make it public static.

Take care when using variables publically available to other classes - it''s rarely a good design choice because it allows you to code highly-coupled classes (changing one class means you have to cascade change things through others). It also makes it hard for the class to fufil it''s contract to encapsulate and look after a variable if other classes can change it under its feet.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
quote:Original post by paulsdsrubbish
botman2 is right, except you can get away with just saying

var = "hello";


d''oh. i knew that...
If you''re using static methods (as in static void main), make sure you make var static as well (static String var = "hello").

This topic is closed to new replies.

Advertisement