M
Mike Barnard
Hi again all.
Another very basic question from a TOTAL newbie. (I have a book and
I'm getting dangerous to myself now.) However unnecesary some of the
methods below are, I'm doing this to try to learn how to pass
information around.
Q: How does one refer to a variable which was declared in a different
method? My logic was to declare it in the class level block but that
brought the error message:
"G:\java\Projects>javac starnumbers.java
starnumbers.java:28: non-static variable numberStore cannot be
referenced from a
static context
numberStore = new int[number] ;
^
1 error"
I know there's lots of lessons for me to learn yet, but as I only get
about an hour an evening if I'm lucky, it may take some time! This
seems to be the point where I need to learn about static and non
static.
/*
Self tutorial.
Get a user to enter 10 numbers.
display the correct number of stars for each number.
Store the numbers in an array.
Print them all in one go.
*/
public class starnumbers
{
int[] numberStore; // A place to store all of the numbers.
//This was originally in main with 'number'.
public static void main (String[] args)
{
int iterations = 10 ; // number of times to loop.
int number = 0;
initialise (iterations) ;
displayInstructions ();
getAKeypress();
//validateNumber();
//displayStars();
}
public static void initialise (int number)
{
numberStore = new int[number] ;
}
public static void displayInstructions ( )
{
System.out.println("V 1.01 - Instructions");
System.out.println("Use the keyboard to enter single numbers");
System.out.println("You need to enter 10 numbers.\n\n");
System.out.print("Start now...");
}
public static void getAKeypress ()
{
//next bit to study...
}
}
Another very basic question from a TOTAL newbie. (I have a book and
I'm getting dangerous to myself now.) However unnecesary some of the
methods below are, I'm doing this to try to learn how to pass
information around.
Q: How does one refer to a variable which was declared in a different
method? My logic was to declare it in the class level block but that
brought the error message:
"G:\java\Projects>javac starnumbers.java
starnumbers.java:28: non-static variable numberStore cannot be
referenced from a
static context
numberStore = new int[number] ;
^
1 error"
I know there's lots of lessons for me to learn yet, but as I only get
about an hour an evening if I'm lucky, it may take some time! This
seems to be the point where I need to learn about static and non
static.
/*
Self tutorial.
Get a user to enter 10 numbers.
display the correct number of stars for each number.
Store the numbers in an array.
Print them all in one go.
*/
public class starnumbers
{
int[] numberStore; // A place to store all of the numbers.
//This was originally in main with 'number'.
public static void main (String[] args)
{
int iterations = 10 ; // number of times to loop.
int number = 0;
initialise (iterations) ;
displayInstructions ();
getAKeypress();
//validateNumber();
//displayStars();
}
public static void initialise (int number)
{
numberStore = new int[number] ;
}
public static void displayInstructions ( )
{
System.out.println("V 1.01 - Instructions");
System.out.println("Use the keyboard to enter single numbers");
System.out.println("You need to enter 10 numbers.\n\n");
System.out.print("Start now...");
}
public static void getAKeypress ()
{
//next bit to study...
}
}