0

What is the proper way to modify a field of outer class? When trying to change from inner static class the value of Artifact it gives an error. There is need to iterate over array list which contains objects of type Artifact; and be able to display for each Artifact (whether it is Coin or Goblet) the value.

 public class Artifact {
        public int value = 0;

        public static class  Goblet extends Artifact{
            value = 5; // Syntax error on token "value", VariableDeclaratorId expected after this token
        }
        public static class Coin extends Artifact{
            value = 10;
        }

    }
Bohemian
  • 412,405
  • 93
  • 575
  • 722
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185

3 Answers3

5

You can't have a statement value = xxx; outside of either a method or a block.

This would work:

public class Artifact {

    public static void main(String[] args) {
        Artifact goblet = new Goblet();
        Artifact coin = new Coin();

        System.out.println(goblet.value); //prints 5
        System.out.println(coin.value); //prints 10
    }

    public int value = 0;

    public static class Goblet extends Artifact {

        {value  = 5;}
    }

    public static class Coin extends Artifact {

        {value  = 10;}
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
1

Firstly, those static classes are not enclosed - they are static.

Secondly, you don't need enclosed classes, you need normal top level ones.

Thirdly, you need a constructor in Artifact that is passed the value to use.

Try this - three separate classes:

public abstract class Artifact {
    public final int value;

    protected Artifact(int value) {
        this.value = value;
    }
}

public class Goblet extends Artifact {
    public Goblet() {
        super(5);
    }
}

public class Coin extends Artifact {
    public Coin() {
        super(10);
    }
}

I chose to make Artifact abstract so you can't create one directly and must make a subclass that passes in a value, and the constructor is protected just to reinforce that (only subclasses can see it).

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • The only reason I did not choose this approach because I need (if possible) all artifact types in one class file, in order to be compact – J.Olufsen May 29 '12 at 14:24
0
public class Artifact {
public int value = 0;

public static class Goblet extends Artifact {

    public Goblet() {
        super();
        value = 5;
    }

    public void modifyOuterClassfield(int someValue) {
        value = 100 + someValue;
    }
}

public static class Coin extends Artifact {

    public Coin() {
        super();
        value = 10;

    }   

    public void modifyOuterClassfield(int someValue) {
        value = 100 + someValue;
    }

}

public int getValue() {
    return value;
}

public void setValue(int value) {
    this.value = value;
}

    public static void main(String[] args) {
    Artifact a = new Coin();
    Artifact b = new Goblet();
    Coin c = new Coin();
    Artifact d = new Artifact();

    System.out.println(a.getValue());
    System.out.println(b.value);
    System.out.println(c.getValue());
    System.out.println(d.getValue());

    a.value = 101;
    b.value = 202;
    c.setValue(303);
    d.setValue(404);

    System.out.println(a.getValue());
    System.out.println(b.value);
    System.out.println(c.value);
    System.out.println(d.getValue());
}

}

Output:

10

5

10

0

101

202

303

404

A static class is not the same as a static variable. Hence instance initialization will do the trick because the compiler will not complain but it is not the complete picture because you can initialise the variables in other ways, unless your inner class is an anonymous inner class, as an anonymous inner class can't have a constructor.

The fields of an outer class are always visible within the inner classes as long as the fields are not declared private!


http://www.javaworld.com/javaqa/1999-08/01-qa-static2.html

http://www.artima.com/designtechniques/initializationP.html

Marky Mark
  • 98
  • 1
  • 1
  • 5