static - Constant Variables in Java -


i'm creating static objects inside class this. want them constant seems did wrong because objects b c same (their x same). can explain why?

public class key { private note[] sequence; private string name;  public key() {     this.sequence = note.notes; }  public key(string name, note[] notes) {     this.sequence = note.notes;     this.name = name;     for(int = 0; < this.sequence.length; i++){         for(int j = 0; j < notes.length; j++){             if(this.sequence[i].equals(notes[j])){                 this.sequence[i].setintensity(0.6);                 break;             }             else this.sequence[i].setintensity((double)0);         }     } }  private static final key c = new key("c", new note[]{note.f2, note.g2, note.a2, note.b2, note.c3, note.d3, note.e3, note.f3, note.g3, note.a3, note.b3}); private static final key csharp = new key("c#", new note[]{note.fsharp2, note.gsharp2, note.asharp2, note.c3, note.csharp3, note.dsharp3, note.f3, note.fsharp3, note.gsharp3, note.asharp3}); private static final key d = new key("d", new note[]{note.fsharp2, note.g2, note.a2, note.b2, note.csharp3, note.d3, note.e3, note.fsharp3, note.g3, note.a3, note.b3}); private static final key eflat = new key("eb", new note[]{note.f2, note.g2, note.gsharp2, note.asharp2, note.c3, note.d3, note.dsharp3, note.f3, note.g3, note.gsharp3, note.asharp3}); private static final key e = new key("e", new note[]{note.fsharp2, note.gsharp2, note.a2, note.b2, note.csharp3, note.dsharp3, note.e3, note.fsharp3, note.gsharp3, note.a3, note.b3}); private static final key f = new key("f", new note[]{note.f2, note.g2, note.a2, note.asharp2, note.c3, note.d3, note.e3, note.f3, note.g3, note.a3, note.asharp3}); private static final key fsharp = new key("f#", new note[]{note.fsharp2, note.gsharp2, note.asharp2, note.b2, note.csharp3, note.dsharp3, note.f3, note.fsharp3, note.gsharp3, note.asharp3, note.b3}); private static final key g = new key("g", new note[]{note.fsharp2, note.g2, note.a2, note. b2, note.c3, note.d3, note.e3, note.fsharp3, note.g3, note.a3, note.b3}); private static final key gsharp = new key("g#", new note[]{note.f2, note.g2, note.gsharp2, note.asharp2, note.c3, note.csharp3, note.dsharp3, note.f3, note.g3, note.gsharp3, note.asharp3}); private static final key = new key("a", new note[]{note.fsharp2, note.gsharp2, note.a2, note.b2, note.csharp3, note.dsharp3, note.e3, note.fsharp3, note.gsharp3, note.a3, note.b3}); private static final key bflat = new key("bb", new note[]{note.f2, note.g2, note.a2, note.asharp2, note.c3, note.d3, note.dsharp3, note.f3, note.g3, note.a3, note.asharp3}); private static final key b = new key("b", new note[]{note.fsharp2, note.gsharp2, note.asharp2, note.b2, note.csharp3, note.dsharp3, note.e3, note.fsharp3, note.gsharp3, note.asharp3, note.b3}); } 

your code create different objects different values, they not constant.

to create constant need declare variable final , additionally need block possibility change it, making object immutable. in case, can done making final fields. more create immutable class need follow rules:

  • don't add setter method

  • declare fields final , private

  • if field mutable object create defensive copies of getter methods

  • if mutable object passed constructor must assigned field create defensive copy of it

  • don't allow subclasses override methods.

if additional informations on immutable objects wrote article on dzone can find @ following link

so code:

final class {     final int x;      public a(int x) {         this.x = x;     };      private final static a = new a(1);     private final static b = new a(2);     private final static c = new a(3); } 

to answer last comment.

in constructor have:

public key(string name, note[] notes) {     this.sequence = note.notes;     ... } 

it means value of sequence common instances of key. if need different sequence values need (basically cloning array note.notes)

public key(string name, note[] notes) {     this.sequence = note.notes.clone();     ... } 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -