Needs explanation plz..

I have observed the following but can not get the actual explanation. If anybody can explain this thenit will be helpfull for my knowledge base.

The problem is like this:

I have two classes A and B like below:

publicclass A

{

privateint i;

publicvoid setI(int x)

{

i=x;

}

publicint getI()

{

return i;

}

}

and

publicclass B

{

publicstaticvoid main(String args[])

{

A a=new A();

a.setI(10);

System.out.println(a.getI());

}

}

I have compiled both classes and execute classB. It is pretty simple to guess the output.

Now I changed the class A so that the permission of the function getI() and setI() is private: as follows:

publicclass A

{

privateint i;

privatevoid setI(int x)

{

i=x;

}

privateint getI()

{

return i;

}

}

Now I just compile class A only.. (I have not compile Class B because it has not been changed.)

Now I guessed that when I will execute class B it will give error since the setI() and getI() methods of class A is private now. But when I execute class B it is able access the setI() and getI() method and gives the output as same as before.

How this is possible?

[2617 byte] By [diptaa] at [2007-10-3 8:56:33]
# 1

There was the same question in another thread the other day. Sadly I can't find it.

My replies then:

- don't run half-compiled code

- I couldn't find a specification saying that the JVM has to do run-time access modifier checks, since they usually only matter at compile-time and would only eat up performance.

CeciNEstPasUnProgrammeura at 2007-7-15 4:06:50 > top of Java-index,Java Essentials,New To Java...
# 2
Then will you say it a bug... or the developers has done it intentionally due to some other reasons
diptaa at 2007-7-15 4:06:50 > top of Java-index,Java Essentials,New To Java...
# 3
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.htmlSince you are creating object of class A in B, they are related. If you make any change in A, that can make some effects in B also. So compile both and see the errors u will get.Cheers
astelaveestaa at 2007-7-15 4:06:50 > top of Java-index,Java Essentials,New To Java...
# 4
> Then will you say it a bug... I said it's not a bug. How can it be a bug if nobody says it shouldn't do that?> or the developers has> done it intentionally due to some other reasonsI told you the reasons.
CeciNEstPasUnProgrammeura at 2007-7-15 4:06:50 > top of Java-index,Java Essentials,New To Java...
# 5

I know that if I compile class B then I will get error which is desirable.

But my question is that the access control modifiers should depend on A.class file not B.class file.

If this the right way I am thinking then changing the class code of A should prevent the access of its own private member.

diptaa at 2007-7-15 4:06:50 > top of Java-index,Java Essentials,New To Java...
# 6
*sigh* Once again:It's only a compile-time check because a runtime check isn't necessary because the check already happened at compile-time (see Generics), and if you run only half-compiled code, you're doing so at your own risk anyway.
CeciNEstPasUnProgrammeura at 2007-7-15 4:06:50 > top of Java-index,Java Essentials,New To Java...