1

I have been having a bit of an issue with creating an Array of Objects and then setting the variables for each object. I create the array and then loop through the array setting the variables but it seems each time I loop through it overwrites all previous variables.

It seems like a basic piece of code and a very irregular bug...

public class Test{

    Bubble[] bubble = new Bubble[2];

    public static void main(String[] args)
    {

        bubble = new Bubble[2];

        bubble[0] = new Bubble();
        bubble[0].setDegree(360);

        bubble[1] = new Bubble();
        bubble[1].setDegree(180)

        System.out.println(bubble[0].printDegree());
        System.out.println(bubble[1].printDegree());
    }

}

That is the basics of it, the other class merely sets up variables and assigns values to them using "bubble[i].setDegree(int);"

The output of the progam gives me:

180
180

What I should be getting is:

360
180

I don't know if I am just rusty and missing something completely obvious or something is going terribly wrong.

4
  • 2
    Please show the definition of the class Bubble. Commented Sep 2, 2013 at 16:49
  • 3
    That's not your actual code - it wouldn't even compile at the moment, as you're trying to access an instance variable (bubble) from a static method. Commented Sep 2, 2013 at 16:50
  • Please post the Bubble class. Commented Sep 2, 2013 at 16:50
  • @captain: Please use proper words. N y r makes no sense to me, even as a native English speaker. I dread to think how incomprehensible it would be for someone who has English only as a second or third language. Commented Sep 2, 2013 at 16:50

2 Answers 2

8

The only possibility which I see is that your field degree in Bubble class is static. And since a static field is shared by all instances, whatever changes you do will be reflected for all the instances. Or your setDegree() or printDegree() method is broken (as pointed out by @JonSkeet in comments).

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

7 Comments

...You were 30secs faster! :D
Could be setDegree() and printDegree() are using different instance variables and the default is 180.
@KennyTM. What do you mean by default is 180?
@RohitJain: class Bubble { int mDegree = 180; ... } (Well one case of what Jon Skeet said.)
@KennyTM. That will be the issue when the setDegree method is not actually doing what it is supposed to do.
|
1

It seems like the variable in class bubble holding the value of degree is static, i. e. it exist per class and not per object.

You could also try that for clarification:

    bubble[0] = new Bubble();
    bubble[0].setDegree(360);

    System.out.println(bubble[0].printDegree());

    bubble[1] = new Bubble();
    bubble[1].setDegree(180)


    System.out.println(bubble[1].printDegree());

3 Comments

Thank you for your reply. It seems that static was causing an issue.
Np. Hope your question showed you the difference between static and non-static.
Sure did, it was something I was on the fence about, but now have a clearer understanding. Cheers.

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.