开发者

"CONST" in java

I have a question about "CONST" in java.

I want to know how to make like "const struct VAL &getVal()" in C-Language.

Here is sample code.

public class test {
   /* VAL is just structure-like class */
   class VAL {
    public int i;
   };
   private VAL  val;

   /* test class functions */
   test() {
    val = new VAL();
    val.i = 1;
   }

   VAL getVal() {
    return val;
   }

   /* main function */
   public static void main(String[] args) {
    test t = new test();
    VAL  t_val;

    t_val = t.getVal();

    t_val.i = 2; /* it should be error if t_val variable is a const */
    System.out.printf("%d %d\n", t.getVal().i, t_val.i);
   }
}

below is C sample code.

struct VAL
{
    int i;
};

const struct VAL &getVal() /* THIS IS WHAT I WANT */
{
    static VAL xxx;
    return xxx;
}

int main()
开发者_JAVA百科{
    const VAL &val = getVal();
    val.i = 0; /* error */

    VAL val2 = getVal();
    val2.i = 0; /* it's not an error, but it's ok because it cannot be changed xxx variable in getVal() either. */

    return 0;
}


final keyword have similar semantics. You cannot change object's reference, however you can change the object itself if it is mutable. I'd suggest to google it and read more about it.


you can't do the exact same thing like in C. Even though you use final keyword, it only prevent the modification of variable reference. t_val.i = 2 will be error if i is declared as final attribute.


Make the member variables of VAL private, and provide public getters. If you still want someone else to be able to change the members after initialization, you can use an interface with only getters, and an implementing class with public setters as well. Then, return the interface of the class, so the caller cannot change its members.

interface ValGetters {
   public int getI();
}

class Val implements ValGetters {
   private int i;
   public int getI() { return i; }
   public void setI(int ii) { i = ii; }
}

Internally, hold a Val. When you want to let someone else use it, return as ValGetters.


const struct VAL &getVal() /* THIS IS WHAT I WANT */

There is no such construct in Java, all answers to the contrary notwithstanding, and especially those that suggest the 'final' keyword.


Better approach is to use enum. They are very powerful. You can use it in switch statement, and can have inner properties as you need with struct. You can look for documentation at http://download.oracle.com/javase/tutorial/java/javaOO/enum.html.


Short answer is you cant, at least not in a general sense. You CAN if you use all primitives(or classes you create that are all primitives), and a very small number of classes(String comes to mind), but you CANNOT do it in a general sense as you have no control over what people do with your objects after calling the getter method. If you want to do this, you have 2 options:

  1. Make all your FIELDS(not your class, your FIELDS, in this case i) final, that way people cannot change them.

  2. Make all your fields private and dont create any public setters(this is the preferred method)

However, going back to my original point:

Suppose val was defined like this:

public class VAL {
        private List<Integer> i=new ArrayList<Integer>();
        public VAL() {
            i.add(3);
         }
        public List<Integer> getI() {return i;}
       };

Now nobody can come along and change the object your list is pointing to, but they can very well change what i is pointing to, but code like this is perfectly valid(and it is valid even if i is marked final):

VAL bob=new VAL();
System.out.println(bob.getI().size());
bob.getI().clear();
System.out.println(bob.getI().size());

The above code will print out 1 0

Meaning that although i didnt change, your list contents did........


use final keyword. It is analogue for const

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜