• Post Reply Bookmark Topic Watch Topic
  • New Topic

pls reply  RSS feed

 
rahul arora
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class a{
private int x;
public static void main(String a[]){
a a1 = new a();
System.out.print(a1.x);
}
}
will this compile
if yes then why,arent we accessing private variable thru class instantiation.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! The Cattle Drive is reserved for questions from people taking the Java training course. I'll move your question to the Java in General (beginner) forum for you. (In future, please use a meaningful topic!)

Dave
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rahul arora:
class a{
private int x;
public static void main(String a[]){
a a1 = new a();
System.out.print(a1.x);
}
}
will this compile
if yes then why,arent we accessing private variable thru class instantiation.


this will compile.
you are calling a1.x within the class where private int a is visible so this will work.If you would have done the same outside this class then this would not have worked.
 
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
Hi,

Welcome to JavaRanch!

Rahul's explanation is correct; I just wanted to point out that while some languages have "object-based encapsulation", Java, C++, and other related languages have "class-based encapsulation." If you're used to a language that limits access to private members to one specific instance of a class, then the Java/C++ approach, where any object may access the private members of any other object of the same class, may surprise you. But the class-based approach sure makes implementing comparison and object-copying methods a lot easier!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Rahul's explanation is correct; I just wanted to point out that while some languages have "object-based encapsulation", Java, C++, and other related languages have "class-based encapsulation."

Except in this weird case, where Java behaves as if it has object-based encapsulation.
 
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
Originally posted by Jesper Young:

Except in this weird case, where Java behaves as if it has object-based encapsulation.


I've added a note to that thread which I hope will help you understand what's happening; it's not object-based encapsulation, at any rate. If you think my note doesn't help, please show me some real code that demonstrates the issue you're discussing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!