0

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.

5
  • 1
    For these goals you can use collections. Commented Apr 8, 2014 at 11:03
  • 2
    Your problem is that you've got three different objects, and you're setting fields in one object, and expecting them to show up in another. This is like painting your friend's house; then going home, looking at your own house, and wondering why it's still the same colour as it was when you left. Commented Apr 8, 2014 at 11:08
  • To further explain what @DavidWallace said: Non-static fields in Java are only available within the instance of a class that they're in. If you DO want to share a value over all instances of a class, you can make a field static, e.g. public static int i;. Commented Apr 8, 2014 at 11:12
  • gstackoverflow>>> Can you be more specific, and why should I use something so specific for seemingly such a basic task? David Wallace>>> Ok so how do I make the variables truly public just like they would be public when declaring in C++ outside main? And what is the normal practice to archeve what I want without using them? I know how it works if I want return no more than 1 variable from the method, but with 2 and more I have little to no idea on how to do that without public variables. Commented Apr 8, 2014 at 11:14
  • Well, I think you only ever intended to have one object, not three. So get rid of class2 and class3 entirely. Don't declare them; don't create them. Now, in main2, instead of writing class2.Initialize(3);, just write Initialize(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 called class1. Commented Apr 8, 2014 at 11:19

1 Answer 1

1

You are creating 3 new objects of the class ADT. Each of them will have member variables initialized to 0.

If you want them to have same values in all the objects, try using static variables. They are associated with the class rather than the object.

----------------------------------EDIT-----------------------------------------
Instead of returning more than one value try this:

  • In your Initialize method, do all the work you want with i and queue.
  • Add two methods in class ADT namely getI and getQueue which returns current values of i and queue> Example:

    public int getI()
    {
        return i;
    }
    
  • Call these methods in your main to get the updated values

If you use static variables, then all the objects of that class will access same varibles.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh of course! Got it. And how would I go about this if I wouldn't wan't to use public variables? I imagine there's gotta be another way for returning more than 1 value.
If you want to return more than one value from a function one way to do it would be to use the Pair class from Apache Commons commons.apache.org/proper/commons-lang/apidocs/org/apache/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.