Jump to content
  • Advertisement

Archived

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

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

This topic is 5093 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
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
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
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!