• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem with Access modifiers

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Issue is like this:
I have one class Prog1.java as stated below
public class Prog1
{

public static void main(String args[])
{
Prog2 p2=new Prog2();
System.out.println("Calling Prog2 Call method");
p2.call(args[0]);
}
}
and another class Prog2.java like below
public class Prog2
{
private int i=10;

public void call(String num)
{
System.out.println("In Call"+(++i + Integer.parseInt(num)));
}
}

I compiled both class and Run Prog1 . The result is obvious. Whatever I pass as argument plus 11. Uptill now its fine.
Now If I change method access modifier of call method in Prog2 class from private to public.i.e Prog2 will now look as
public class Prog2
{
private int i=10;

private void call(String num)
{
System.out.println("In Call"+(++i + Integer.parseInt(num)));
}
}

Just compile Prog2. Ideally Prog1 should not be able to access call method now. But it will still run. Compiling Prog1 will give error about access modifier but if you do not compile Prog1.java (As it is not required due to no change has happened in Prog1) and run it as it is , it will run fine.

Can someone explain this behavior?
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to be common issue and no one is really interested to answer this.
Request to please answer this.
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically Prog1 class file is still referring to the old Prog2 class file. Compiling the Prog2 is surely fine because it's legal, but that's not the case with Prog1 since it's illegal by now.

Try to clean everything and build from scratch, I'm pretty sure it won't compile.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try searching the forum. This topic has been discussed very recently. Probably this also had to do with the JDK (SUN) version you have. This would work with 1.5 and not with 1.6.
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer Freddy , I recompiled Prog2 not Prog1 as Prog1 has not been changed at all . So no need to recompile it. And one more thing it is loading a fresh Prog2 class. As if you change something in Call method and recompile Prog2. Prog1 will take that new logic into account. Its just it is not taking Access modifier change into consideration.

To answer Anupam, I am using JDK1.5
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kem cho, Kartik bhai,maza ma ??

Well,Before one week I posted the same Question under "I can access private members.". If we compile both files, then it gives compile time error.

I think this is unusual behavior or may be bug for window platform. My friend uses Linux system and I sent my .class files to him.When he had executed those file,he was getting runtime error i.e IllegalAccessException.

So there is not satisfactory answer, hope someone provide us with rich explanation over this
[ June 28, 2007: Message edited by: Bharat Makwana ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what the documentation that comes with the JDK is for, folks; this is all explained very clearly, for example, here.

This, like many other things, is handled by the installed SecurityManager. If there is no SecurityManager installed -- and when you start up the plain old command-line java.exe, there is not -- then this check isn't done at runtime. If you want the check to be done, then you need to install a security manager, which you can do by using "-Djava.security.manager" on the command line, as in

java -Djava.security.manager MyApplication

If you add this command-line switch when trying your experiment above, you'll see it fails. The default SecurityManager implementation properly enforced access modifiers.

Java applications that can host potentially untrusted code -- any kind of container, like an applet container, servlet container, RMI server, etc -- should (and does) install a security manager to handle this sort of stuff.
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to run Prog1 with security manager option enabled by following command line but it is still having same problem . It is not giving exception as stated.

java -Djava.security.manager Prog1 5
 
Yeah, but is it art? What do you think tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic