Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need to explain  RSS feed

 
Paul Savostin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

i'm new in Java and don't speak english very well but i think you understand ))

so the question is

say we have next class

it's working only in this class, if the same method move to any other class we can't see variable 'a'
can anybody explain this to me ?

thanks in advance!
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Savostin wrote:
so the question is

say we have next class

it's working only in this class, if the same method move to any other class we can't see variable 'a'
can anybody explain this to me ?


Well, that is what "private" means. It means that it can only be accessed by code within the same class. It is private to the class.

Only methods, constructors,or initializers within that class (or methods, constructors, or initializers of inner classes) can access the class.

Henry
 
Paul Savostin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the answer but I think this is the classic definition of the private modifier.

But in this case, there is a "violation" of access, because the compiler "sees" that an instance
located in the method's parameter has the same type Test as the class in which this method is in.
And it allows access to the class variable using a dot . It's just my opinion, but where can I get more reliably explaination?

thanks anyway
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul. Welcome to the Ranch!

It isn't a violation. The definition of private is that it's accessible within the same class. You are accessing it from within the same class, so that's allowed. Not the same object, the same class - that's how private works. There are languages that allow you to limit access to within the same instance, but Java isn't one of them.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
It isn't a violation. The definition of private is that it's accessible within the same class. You are accessing it from within the same class, so that's allowed. Not the same object, the same class - that's how private works. There are languages that allow you to limit access to within the same instance, but Java isn't one of them.



C++ (which is what Java is somewhat based on) isn't one of them either. In C++, private is also private to the class -- not the instance. IMO, it would probably be difficult to implement copy constructors, or comparable methods, if private meant private to the instance.

Henry
 
Paul Savostin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All i can get is that the key point is that another(or even it can be the same) Test object is located in the same Test class.
it's more exactly looks like definition of private modifier.
That's all I think. Thanks guys

Glad to join your ranch!
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right. Code in the Test class can access private members in the Test class, that's what "private" means. Objects have nothing to do with it.

(It would be quite difficult -- perhaps impossible -- for the compiler to determine whether one object of a class was trying to access a private member of a different object of that class.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!