• Advertisement

Archived

This topic is now archived and is closed to further replies.

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

This topic is 5001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
You cannot. A variable declared inside a function is visible only inside that function.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
And how can I make a variable visible to all functions ?
I cant !!! ???
there is no way ?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Just define a public static class variable, then that variable is global. You can access that variable by CLASS_NAME.VARIABLE_NAME.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
quote:
Original post by paulsdsrubbish
botman2 is right, except you can get away with just saying

var = "hello";


d''oh. i knew that...

Share this post


Link to post
Share on other sites
If you''re using static methods (as in static void main), make sure you make var static as well (static String var = "hello").

Share this post


Link to post
Share on other sites
While you are explaining this, i have a question. What does transient mean? And I''m also not quite sure about protected.
Thanks..

Share this post


Link to post
Share on other sites
Variables marked as transient are not saved when serialising a class, so are usually volatile or calculated from the other data (such as the length of a list).

Protected vars are only accessible to the owning class and any inherited classes.

Share this post


Link to post
Share on other sites
99 times out of 100, when you think you "need" global access to a single variable, you should re-think your design methodology, not try to force it to happen.

The best advice for global OBJECTS would be to use Singletons.

Share this post


Link to post
Share on other sites

  • Advertisement