4

Ok so basiclly I have three classes:

  • main class
  • Apple(two constructors)
  • Pie

in the main class I do:

Apple apple = new Apple(String one, String two);

Then the Apple class sets them globally:

public Apple()
{
    //empty constructor
}
public Apple(String one, String two)
{
    this.one = one;
    this.two = two;
}

Then in the Pie class I do:

Apple apple = new Apple();

Then if I try to access the variables 'one' or 'two' from the Pie class they return null. Can someone help me?

JDerksen
  • 41
  • 1
  • 4
  • 1
    It looks like you aren't setting the one or two varialbes in the Apple class. Also, public variables is IMO a questionable design. – John Ericksen Feb 06 '13 at 20:02
  • You have two separate objects of the same class `Apple`. Once you set some fields of one object, fields of another object remain unchanged. – Mikhail Vladimirov Feb 06 '13 at 20:02

3 Answers3

10

You are creating two different objects. If you want all Apple objects to have the same parameter, declare them as static. Otherwise the behavior is correct.

More specifically, the apple that you create in the main class, will have the desired values in it's parameters. The second apple, that is created in the Pie class (and it is a different object i.e. another instance of the Apple class), since it is constructed without any parameters, the default constructor (i.e. public Apple()) will be called, and the values will return null.

To see the difference between a static and a non-static variable do the following:

class Apple {
    int var;
}

Apple apple1 = new Apple();
apple1.var = 10;
Apple apple2 = new Apple();
apple2.var = 5;
System.out.println(apple1.var+"\t"+apple2.var);

Prints:

10     5

But if it is static you will get

class Apple {
    static int var;
}

Apple apple1 = new Apple();
apple1.var = 10;
Apple apple2 = new Apple();
apple2.var = 5;
System.out.println(apple1.var+"\t"+apple2.var);

The output will be:

5     5

For more info on when to use static or not, have a look at:

Java: when to use static methods

Community
  • 1
  • 1
user000001
  • 32,226
  • 12
  • 81
  • 108
  • +1, but just a small nitpick. Instances of a class don't each *have* a copy of the `static` variable. They belong to the class itself and not to any objects created from it. – asteri Feb 06 '13 at 20:05
  • I just made my 'one' and 'two' variable static and it fixed my issue. I never really understood what static does but now it makes sense. – JDerksen Feb 06 '13 at 20:09
  • 3
    Be very careful when using statics, especially if you do not understand them. It sounds like using statics in this case will just mask the problem, not fix it. – John Ericksen Feb 06 '13 at 20:13
  • Can't speak for @johncarl, but I would say that there is nothing wrong with static, but you should use them only when it makes sense. More info [here](http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods). – user000001 Feb 06 '13 at 20:24
  • Right, there is nothing wrong with statics, but often they are misused or unnecessary. In this case I worry that the order in which you call the two constructors would, in some cases, not set the static values and result in the Apple object containing null values. – John Ericksen Feb 06 '13 at 20:55
1

For the default constructor, to have one or two be not null you need to set the apple instance with some default non-null values:

class Apple {
    public Apple()
    {
        this("1", "2");
    }
    public Apple(String one, String two)
    {
        this.one = one;
        this.two = two;
    }
    //...
}
John Ericksen
  • 10,995
  • 4
  • 45
  • 75
0

So the Apple that belongs in the Pie class is a different Apple from the one that you create in Main. So when you call the empty constructor, one and two aren't set in your pie Apple, while they are in the main Apple. If you really want them to be the same, try referring to main's Apple from Pie, or make Apple static.

Peter
  • 1,349
  • 11
  • 21