This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I can access private variable?? pl give me reason..  RSS feed

 
Markandey Sahu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I can access the private variable. how?? i am showing now
write these two class and compile and run
1st class file name:MyClass.java

2nd class file name: Test.java

compile and run you will get out put:
hello world

now magic is here make following changes in MyClass.java

(means now your MyClass.java become following)

now be careful compile only MyClass.java file not the Test.java file
after compilation run Test class using command (java Test)

now you will get output:
my private variable
MyClass private method

Note: Please run the following application in your console
don't use any editor .. eclipse or other else

if you get reason for this behavior pls answer me
thanks in advance
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
you are off topic, here. However, consider that the visibility check is done by the compiler and NOT at runtime.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see what's off-topic about this. Seems a perfectly reasonable beginner question, doesn't it?

Basically, if you change and re-build part of an application, and not other parts that would be affected, you can mess it up in a variety of ways, including this one. That's a mis-build and not really terribly interesting beyond that, I think.

In situations where you want to prevent modified code being put into your application, Java has various protection mechanisms available, like SecurityManager, Jar signing, and all sorts of things that I'm afraid I never use, so can't advise further on.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Discussed to death here. Can you please let us know who is putting this bee in your bonnet, so we can look out for it in the future? Both people who posted this question posted it multiple times in multiple forums, so I'm thinking it's a job interview question or something.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!