import java.lang.*;
int i = 0;
public static void main(String[] a)
{
System.out.println(i);
}
Java .... which i forget....
hi...
is there a global variable like:
Thanks
You don''t need to import java.lang, it''s imported by default.
I''m not sure what your question is exactly but the main method and the variable declaration for i need to go in a class:
I''m not sure what your question is exactly but the main method and the variable declaration for i need to go in a class:
public class Whatever { public static void main(String[] args) { int i=0; System.out.println(i); }}
As Dobbs has stated, you need to place the variable declaration inside a class declaration. To define a global variable you declare it public, ie public int i = 0;. I think this is what you mean. It can then be referred to from any class by instanceOfClass.i, if you see what I mean. So if you declare a new instance of class Example with
Example myExample = new Example();
myExample.i = 5;
I think this is what you mean.
Stu
Example myExample = new Example();
myExample.i = 5;
I think this is what you mean.
Stu
if you needed a variable to act as a global, put you could put it in a class and make a static method to return the value
Then you could use this method in any code where you need the value. Of course you need to import it.
eh, just an idea. Been over a year and a half since I wrote Java professionally.
[edited by - progrmr on May 7, 2003 2:02:52 PM]
public class MyUtilityClass{ public static int GetThisNumberAllTheTime() { return 5; }} then you could call it from your code like this: if (iCounter > (MyUtilityClass.GetThisNumberAllTheTime()){ ...do something here}
Then you could use this method in any code where you need the value. Of course you need to import it.
eh, just an idea. Been over a year and a half since I wrote Java professionally.
[edited by - progrmr on May 7, 2003 2:02:52 PM]
What if you made the variable static? Since in this case you going to only have the one class that has your main.
would this work?
public class Foo{ private static int number = 10; public static void main(String args[]) { //do stuff with number }}
would this work?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement