@agraham the question is just that. The private variable can be used, for example, to store the value of a property; this is a public routine which will value the private variable as it wants.By only letting the outside world access public things you can control what they can and can't do to your class.
Class_Globals - This sub is similar to the activity Globals sub. These variables will be the class global variables (sometimes referred to instance variables or instance members).Erel says (my boldening)
Most likely I don't understand you; or viceversaalso C++, TypeScript, C# etc. Sorry but we have 50 years of computer science and language design which has established these norms.
No Java expert here, but a short demo seems to disprove your statement.Java has the encapsulation system and so it shouldn't work if we understand it well
class First
{
private int a=10;
public static void main(String[] args)
{
First first1=new First();
Second second1 = new Second();
Second second2 = new Second();
System.out.println(first1.a); //does not complain - neiter compile nor runtime
//System.out.println(second1.priv); //will throw private access during compile
System.out.println(second1.pub);
System.out.println(second2.getPrivate());
second1.changePriv(second2);
System.out.println(second2.getPrivate());
}
}
class Second
{
private int priv=20;
int pub=30;
public Second() {}
public void changePriv(Second sec) {
sec.priv = 55;
}
public int getPrivate() {
return priv;
}
}
10
30
20
55
No, I'm afraid that I cannot understand what you are trying to illustrate. I can see no reason why a public Sub cannot access a private instance variable.Impossible that the example in post #8 is not clear!
I'm afraid that I still don't understand this. How could you access mSalary of all the instances?But if I had a public method in that class which can access OTHER instances of clsPerson (a public mapPersons in B4XMainPage), I could access mSalary of all the instances in the mapPersons Map, and this is wrong!!!
No, sorry, I had misread. Even on instances created outside the class I can access their private variables, from inside an instance of that class.Instances of a class instantiated inside the same class have access
And this is precisely the problem: I can! And I shouldn't be able to!I'm afraid that I still don't understand this. How could you access mSalary of all the instances?
As I've shown with my second demo, Java seems to break your construct just as B4X does. So my question is, what language adheres to the construct you propose?and this is wrong!!! I should not be able to access mSalary, ever! I have to use the property.
Sorry, I haven't looked at your source because I know little about Java but I trust you.As I've shown with my second demo, Java seems to break your construct just as B4X does. So my question is, what language adheres to the construct you propose?
I dont "want" it - and obviously I don't ask Erel to do it, it's a Java problem - it's that it would be logical: what is private in an object must be private from everywhere.whereas you would like a language that has object level access to private members.
Technically, this "problem" presents itself in any OOP language that implement class level access instead of object level access. As of now, I know of at least 2 other languages besides Java that behave that way.it's a Java problem
I wasn't trying to do something, I was trying to explain what I meant about a bug, apparently it's common to many object-oriented languages. Because this is a bug, it is incorrect that private members of an instance can be accessed.Looking at your sample code, I see what you are trying to do
VB Net and C#? Same "family".As of now, I know of at least 2 other languages besides Java that behave that way.
It's not a bug. It's a design implementation decision. It's something to be aware of (which thanks to you, I am now aware of). It's not how you expected it to work, but that is because the implementation that B4X chose is not the one you expected, not because there is a bug in B4X.Because this is a bug, it is incorrect that private members of an instance can be accessed.
No B4X and C#. And for me B4X <> Java. The code generation for B4X can also be Objective C, C, and who knows what else in the future.VB Net and C#? Same "family".
It makes no sense, it is not logical, it is not useful, it can only be harmful.It's not a bug. It's a design implementation decision.