0

I've written a very small class [that have to grow!] but I suddenly see something "strange".

This is the class:

class Chara{
  private boolean flag_loaded;
  private boolean flag_rage, flag_shield;
  private int int_rage, int_shield;
  Chara(String fileName){
    flag_loaded = true
    if(flag_loaded){
      flag_rage = false;
      int_rage = 0;
      flag_shield = false;
      int_shield = 0;
    }
  }
  boolean rage(){return flag_rage;}
  boolean shield(){return flag_shield;}
  void add_rage(int toAdd){flag_rage = Bool(int_rage += limitMin(toAdd, 0));}
  void add_shield(int toAdd){flag_shield = Bool(int_shield += limitMin(toAdd, 0));}
  void sub_rage(int toAdd){flag_rage = Bool(int_rage -= limitMin(toAdd, 0));}
  void sub_shield(int toAdd){flag_shield = Bool(int_shield -= limitMin(toAdd, 0));}

As you can see all the variables are set as private.

NOTE: limitMin() return the same type variable, in this case 0 if toAdd is lower. Bool() is simply a cast.

This is a piece of main:

Chara chara = new Chara("lol");
print(chara.int_shield, (chara.shield()) ? COL_GREEN : COL_RED);
print(chara.flag_shield, (chara.shield()) ? COL_GREEN : COL_RED);

Nothing is done between declaration, instance and printing.

This piece of code is supposed to print in green the value of the shield and if there is or not the shield but it should not print anything because of int_shield and flag_shield that are supposed to be private.

The result is a nice 0 and a nice false in red...

Why is this happening? I am doing something wrong?

In despair I tried making the variables protected but as I expected nothing happened.

EDIT: added entire Chara class.

ingroxd
  • 995
  • 1
  • 12
  • 28
  • 2
    What is the `Chara();` line in your code supposed to do? – Matt Ball Oct 25 '13 at 23:23
  • Setting everything to 0 [for `int`s] and false [for `boolean`s] – ingroxd Oct 25 '13 at 23:25
  • Why? That's what happens by default. How is that even syntactically valid...? – Matt Ball Oct 25 '13 at 23:34
  • @MattBall call them "debugging purposes", now the problem is why I can access private variables! – ingroxd Oct 25 '13 at 23:37
  • I would guess that main() is in your class, and can therefore access the variables. Don't post any more explanations or code snippets -- boil down the above to a runnable class, and run it. 2-to-1 you will then see the answer yourself; if not, you will have something reasonable to post here. – arcy Oct 25 '13 at 23:52
  • No, the structure is Class{subclass{}; main();} and main can read subclass variables. – ingroxd Oct 26 '13 at 00:15
  • 1
    @IngrossoD In that case, http://stackoverflow.com/questions/1801718/why-can-outer-java-classes-access-inner-class-private-members should help. – Dennis Meng Oct 26 '13 at 02:39

1 Answers1

1

Your main method must be declared in your Chara class, or in some enclosing class of the Chara class. That would explain why it can access the private fields of a Chara instance directly. The Java access rules would prevent access to a private variable in all other circumstances.

An inner / nested class is allowed to access the private variables of an enclosing class, and vice versa. (At least, it is allowed from the perspective of the "access" rules. There are various other rules that may prevent / limit this.)

I'm sure there is no "undefined behaviour" here. Just behaviour that you don't understand.


If this does not explain the behaviour, I don't understand your question / what your code really looks like. Please replace the code snippets on your Question with an SSCCE if you want further help.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216