I got kinda lost and confused with this all basic JAVA stuff.
Let's say I want a method to assign values for array and be able to somewhat return/remember number of assigned values, each value seperately depending on index, and whole array of assigned values in one piece so that I'll be able to use that info in other methods.
Here is the code:
public class ADT {
public int i;
public int queue[] = new int[100];
public int Initialize(int n){
for(i=1; i<=8; i++){
queue[i]=(i/4 + 10/i)*i;
System.out.println(queue[i]);}
//public getI(int i) {this.i=i;}
return queue[n];}
public int getI() {
return i;}
public void main2() {
ADT class2 = new ADT();
class2.Initialize(3);
ADT class3 = new ADT();
class3.getI(); // DOESN'T RETURN 8
System.out.println();
System.out.println(i); // DOESN'T WORK (0)
}
public static void main(String[] args) {
ADT class1 = new ADT();
class1.main2();}}
In other words, I want to be able freely manipulate with this array from any method, but with current cod e I can access the seperate index values by calling Initialize(x), but to print out the whole array I can only with "for loop", and even then I need to access "i" (which could be initialized via scanner in latter code stages, so don't assume it's a constant) to know how many values in array have been initialized, but I can't return it as I already returned the array[n], and I can't call directly for it, (despite it being public variable) as i becomes 0 (old value of 8 disappears), just like when trying to call directly let's say queue[3], it won't give me the value I need. But what's the point then of public variables if they become zero'ed out after method completes? In my book, if I run the method which changes the value of some public variable, that changed value should remain when I call the variable afterwards from another method. Yet the logic in JAVA seems to be different.
static, e.g.public static int i;.class2andclass3entirely. Don't declare them; don't create them. Now, inmain2, instead of writingclass2.Initialize(3);, just writeInitialize(3);. This will initialise the current object, instead of a new one - and the current object at that particular point in your program will be the one you calledclass1.