How is this private variable accessible?
A private member is accessible from any method within the class in which it is declared, regardless of whether that method accesses its own (this
) instance's private member or some other instance's private member.
This is stated in JLS 6.6.1:
...Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
This feature of Java allows you to write methods that accept an instance of the class as an argument (for example - clone(Object other)
, compareTo(Object other)
) without relying on the class having non private getters for all the private properties that need to be accessed.
private member variable is accessed by another object of same class?
you are not accessing c.value outside of the c object.
What you are doing is accessing a private variable of a class in the same class. Because the c variable is of exactly the same type as the class, you can access private variable c.value in that class.
Imagine the following situation
public class HelloWorld
{
public static void main(String[] args)
{
OtherClass myObject = new OtherClass(7);
OtherClass yourObject = new OtherClass(4);
yourObject.value = 23;
System.out.print(myObject.compareTo(yourObject));
}
}
public class OtherClass
{
private int value;
OtherClass(int value)
{
this.value = value;
}
public int compareTo(OtherClass c)
{
return this.value - c.value;
}
}
Your code would not compile because value is not accessible from any class other than OtherClass. However, if you try to access c.value you will certainly succeed in doing that.
You would understand better if you study encapsulation more, and a good source of information is the [official documentation](https://docs.oracle.com/javase/tutorial/java/concepts/object.html"Oracle Documentation")
Angular2 - should private variables be accessible in the template?
UPD
Since Angular 14, it is possible to bind protected
components members in the template. This should partially address the concern of exposing internal state (which should only be accessible to the template) as the component's public API.
No, you shouldn't be using private variables in your templates.
While I like the drewmoore's answer and see perfect conceptual logic in it, implementationwise it's wrong. Templates do not exist within component classes, but outside of them. Take a look at this repo for the proof.
The only reason why it works is because TypeScript's private
keyword doesn't really make member private. Just-in-Time compilation happens in a browser at runtime and JS doesn't have any concept of private members (yet?). Credit goes to Sander Elias for putting me on the right track.
With ngc
and Ahead-of-Time compilation, you'll get errors if you try accessing private members of the component from template. Clone demonstration repo, change MyComponent
members' visibility to private and you will get compilation errors, when running ngc
. Here is also answer specific for Ahead-of-Time compilation.
Why private variable declared outside the class can be access Class on the same file?
private
is intended for use inside a type declaration. Otherwise it has no meaning. When private
is used in a place where it has no meaning, it is reinterpreted as fileprivate
. That is what happens here. This code is in the same file so the fileprivate
variable is visible.
Related Topics
How to Demonstrate Java Multithreading Visibility Problems
Call a Method of Subclass in Java
Using Java Map for Range Searches
Are Java Primitive Ints Atomic by Design or by Accident
Composing Swing Components: How to Add the Ability to Add Actionlisteners
Java Class.Cast() VS. Cast Operator
Java: Convert String to Timestamp
Converting List<Integer> to List<String>
Why Does the "Protected" Modifier in Java Allow Access to Other Classes in Same Package
How to Set Working Directory with Processbuilder
Why Is Javac Failing on @Override Annotation
Why Only 1 Public Class in Java File
Getting Mail from Gmail into Java Application Using Imap
How to Define Multiple Jbutton Actions from a Different Class
Having a 3Rd Party Jar Included in Maven Shaded Jar Without Adding It to Local Repository