Questions tagged [private-members]

In object oriented programming, private members are those data fields, properties, or methods of a class that are only accessible from within the class itself.

691 questions
560
votes
41 answers

Private properties in JavaScript ES6 classes

Is it possible to create private properties in ES6 classes? Here's an example. How can I prevent access to instance.property? class Something { constructor(){ this.property = "test"; } } var instance = new…
d13
  • 9,817
  • 12
  • 36
  • 44
201
votes
10 answers

Why can outer Java classes access inner class private members?

I observed that Outer classes can access inner classes private instance variables. How is this possible? Here is a sample code demonstrating the same: class ABC{ class XYZ{ private int x=10; } public static void main(String...…
Harish
  • 7,589
  • 10
  • 36
  • 47
199
votes
25 answers

Accessing private member variables from prototype-defined functions

Is there any way to make “private” variables (those defined in the constructor), available to prototype-defined methods? TestClass = function(){ var privateField = "hello"; this.nonProtoHello =…
morgancodes
  • 25,055
  • 38
  • 135
  • 187
157
votes
10 answers

Why are private fields private to the type, not the instance?

In C# (and many other languages) it's perfectly legitimate to access private fields of other instances of the same type. For example: public class Foo { private bool aBool; public void DoBar(Foo anotherFoo) { if…
RichK
  • 11,318
  • 6
  • 35
  • 49
150
votes
5 answers

Why can I use auto on a private type?

I was somehow surprised that the following code compiles and runs (vc2012 & gcc4.7.2) class Foo { struct Bar { int i; }; public: Bar Baz() { return Bar(); } }; int main() { Foo f; // Foo::Bar b = f.Baz(); // error auto b =…
hansmaad
  • 18,417
  • 9
  • 53
  • 94
118
votes
7 answers

Why do objects of the same class have access to each other's private data?

Why do objects of the same class have access to each other's private data? class TrivialClass { public: TrivialClass(const std::string& data) : mData(data) {}; const std::string& getData(const TrivialClass& rhs) const { return…
Keith
  • 1,777
  • 3
  • 15
  • 20
118
votes
8 answers

Access to private inherited fields via reflection in Java

I found a way to get inherited members via class.getDeclaredFields(); and acces to private members via class.getFields() But i'm looking for private inherited fields. How can i achieve this?
benzen
  • 6,204
  • 4
  • 25
  • 37
84
votes
11 answers

Private members in CoffeeScript?

Does somebody know how to make private, non-static members in CoffeeScript? Currently I'm doing this, which just uses a public variable starting with an underscore to clarify that it shouldn't be used outside of the class: class Thing extends…
thejh
  • 44,854
  • 16
  • 96
  • 107
79
votes
27 answers

Can I access private members from outside the class without using friends?

Disclaimer Yes, I am fully aware that what I am asking about is totally stupid and that anyone who would wish to try such a thing in production code should be fired and/or shot. I'm mainly looking to see if can be done. Now that that's out of the…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
78
votes
16 answers

Hiding members in a C struct

I've been reading about OOP in C but I never liked how you can't have private data members like you can in C++. But then it came to my mind that you could create 2 structures. One is defined in the header file and the other is defined in the source…
Marlon
  • 19,924
  • 12
  • 70
  • 101
76
votes
5 answers

Which is better between a readonly modifier and a private setter?

I've been working on creating a class and suddenly a thought came to my mind of what is the difference between the two codes: public readonly string ProductLocation; AND public string ProductLocation { get; private set; } Can you guys…
Allan Chua
  • 9,305
  • 9
  • 41
  • 61
69
votes
4 answers

Private Variables and Methods in Python

When should I use _foo (an underscore) or __bar (double underscore) for private members and methods in Python?
57
votes
5 answers

What is the use of private static member functions?

I was looking at the request parser from the boost::asio example and I was wondering why the private member functions like is_char() are static? : class request_parser { ... private: static bool is_char(int c); ... }; It is used in the…
rve
  • 5,897
  • 3
  • 40
  • 64
49
votes
8 answers

How can I test private members and methods of classes?

I am trying to do unit testing (using the Boost unit testing framework) on a C++ class called VariableImpl. Here are the details. class Variable { public: void UpdateStatistics (void) { // compute mean based on m_val and update m_mean; …
Jir
  • 2,985
  • 8
  • 44
  • 66
46
votes
21 answers

Should I document my private methods?

Private methods documentation can only be seen by who has access to the source code. Is it worth the effort spent on it?
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
1
2 3
46 47