• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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 ]
 
I just had the craziest dream. This tiny ad was in it.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic