Archived

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

Java .... which i forget....

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

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:


  
public class Whatever {
public static void main(String[] args) {
int i=0;
System.out.println(i);
}
}

Share this post


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

Share this post


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

    
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]

Share this post


Link to post
Share on other sites
What if you made the variable static? Since in this case you going to only have the one class that has your main.


  
public class Foo
{
private static int number = 10;

public static void main(String args[])
{
//do stuff with number

}
}


would this work?

Share this post


Link to post
Share on other sites