Android Question Private class variable "visibility".

LucaMs

Expert
Licensed User
Longtime User
1629730416831.png


This happens only inside the class itself (but anyway it shouldn't).
 

LucaMs

Expert
Licensed User
Longtime User
By only letting the outside world access public things you can control what they can and can't do to your class.
@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.

Erel says (my boldening)
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).

Exactly: "instance members", I should not be able to access a private instance variable from either outside or inside the class, because the object I use in the class is an instance of that class.

Impossible that the example in post #8 is not clear! šŸ˜³
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Java has the encapsulation system and so it shouldn't work if we understand it well
No Java expert here, but a short demo seems to disprove your statement.
First class module
Java:
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());
}
}
Second class module
Java:
class Second
{
private int priv=20;
int pub=30;
public Second() {}
public void changePriv(Second sec) {
  sec.priv = 55;
}
public int getPrivate() {
  return priv;
}
}
Result:
10
30
20
55
 

Attachments

  • demo2.zip
    572 bytes · Views: 138
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
1629738119368.png


Now, If I had an instance objPerson in B4XMainPage, rightly I COULD NOT access mSalary.

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!!! I should not be able to access mSalary, ever! I have to use the property.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Impossible that the example in post #8 is not clear! šŸ˜³
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.

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!!!
I'm afraid that I still don't understand this. How could you access mSalary of all the instances?
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Thank you @LucaMs.

You have shown me something that I (wrongly) had assumed was not true.

Instances of a class instantiated inside the same class have access to all (public and private) members (properties and methods) of that class.
But instances of a class instantiated outside its class have only access to public members of the class.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
and this is wrong!!! I should not be able to access mSalary, ever! I have to use the property.
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?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
According to this post, C# behaves just like Java and B4X. Looks like these 3 languages (there could be more) implement class level access to private members, whereas you would like a language that has object level access to private members.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
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?
Sorry, I haven't looked at your source because I know little about Java but I trust you.

It is not something that I propose; an instance variable (Global_Process) must always be invisible if it is declared Private. And it is actually invisible but not in case you access other instances of the same class from within the class.

I understood that this is also the case in Java and I tried it myself with VB Net; there is nothing to be done, unfortunately, but it is wrong; it is "risky" to have access to variables that should be hidden.

The example of post 8 is perhaps a logical example.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Looking at your sample code, I see what you are trying to do. Now that we know that we have a class level access language when it comes to private members, the workaround is to create a clsPersons class. This class holds the list of people, instead of just a map (even though, internally the new class can use a map). This new class has a LogPersons methods that calls each clsPerson methods/variables. In this case, the methods/variables have to be Public, or the calls will fail (since clsPersons is not the same class as clsPerson).
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
it's a Java problem
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.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Because this is a bug, it is incorrect that private members of an instance can be accessed.
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.
VB Net and C#? Same "family".
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.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
@LucaMs, your example code from #26 is very lucid. So I revise my statement.

Instances of a class have access to all (public and private) members (properties and methods) of all other instances inside the context of their class definition.

Definitely not what I would expect, irrespective what other languages do.
It is a good thing that the values of those (private) properties are specific to each instance.
 
Last edited:
Upvote 0
Top