• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing private members

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,

I have following two .java files in the same directory and CLASSPATH is set to "."(Current working directory)
Please Note : I am compiling and running from command prompt

// Use.java

code:


class Use {

public static void main(String [] args){
Provider p = new Provider();
System.out.println(p.name);
p.getMessage();
}

}



//Provider.java

code:

class Provider {
String name = "hi how are you? Bharat"; // Line #1
void getMessage(){ // Line #2
System.out.println("You have Message !!!");
}

}



Now, I compile these two files and if I run Use class,it prints
hi how are you? Bharat
You have Message

Everything is fine upto this. Now I change access modifier at Line #1 and Line #2 from default to private in Provider.java file, I also change variable name's value for "hi how are you? Bharat" to " are you there ?"(at Line #1). I also delete Provider.class file.

After these modifications, I recompile Provider.java file (Please Note:I have just recompile Provider.java file, NOT Use.java) and then if I run , Use class file,it prints

are you there ?
You have Message

I have no idea, how's this possible ?? Can anyone explain me what's going on behind the scene ?

Thank You
[ June 21, 2007: Message edited by: Bharat Makwana ]
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post duplicates!
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bharat Makwana:
Hello Everyone,

I have following two .java files in the same directory and CLASSPATH is set to "."(Current working directory)
Please Note : I am compiling and running from command prompt

// Use.java

code:


class Use {

public static void main(String [] args){
Provider p = new Provider();
System.out.println(p.name);
p.getMessage();
}

}



//Provider.java

code:

class Provider {
String name = "hi how are you? Bharat"; // Line #1
void getMessage(){ // Line #2
System.out.println("You have Message !!!");
}

}



Now, I compile these two files and if I run Use class,it prints
hi how are you? Bharat
You have Message

Everything is fine upto this. Now I change access modifier at Line #1 and Line #2 from default to private in Provider.java file, I also change variable name's value for "hi how are you? Bharat" to " are you there ?"(at Line #1). I also delete Provider.class file.

After these modifications, I recompile Provider.java file (Please Note:I have just recompile Provider.java file, NOT Use.java) and then if I run , Use class file,it prints

are you there ?
You have MessageI have no idea, how's this possible ?? Can anyone explain me what's going on behind the scene ?



Thank You

[ June 21, 2007: Message edited by: Bharat Makwana ]



Hi Bharat,
After you recompile the provider class the new class file is used which obviously result in the output what you are getting.



Regards
Nik
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have changed access modifiers during recompilation.

Now I change access modifier at Line #1 and Line #2 from default to private in Provider.java file, I also change variable name's value for "hi how are you? Bharat" to " are you there ?"(at Line #1). I also delete Provider.class file.

So when I ran Use.java , it should give me IllegalAccessException

Right ?
[ June 21, 2007: Message edited by: Bharat Makwana ]
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic