• Post Reply Bookmark Topic Watch Topic
  • New Topic

firstName has private access in Person  RSS feed

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i'm trying to access a private String from another class is that possible?
 
Sheriff
Posts: 21137
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only through reflection, and only if the security manager allows it. In general, it's a case of bad design - either by the person that created the class for not providing getters / setters or by you for trying to access something you really shouldn't. (Usually it's the former.)
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Only through reflection, and only if the security manager allows it. In general, it's a case of bad design - either by the person that created the class for not providing getters / setters or by you for trying to access something you really shouldn't. (Usually it's the former.)

If a data member is private, then following won't work:
object.privateMember; //error, privateMember is private

Like Rob said above, if you are supposed to have access to this data then there will be a getter method, and it will probably look something like this:
object.getPrivateMember();

If you can't find a method like the one above, then someone needs to consider redesigning something.
 
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if the class is an internal class.
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks now I can access private things using getters!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!