Questions tagged [private]

Private is a way of encapsulation in object-oriented programming.

Private is a way of encapsulation in object-oriented programming.

2890 questions
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
192
votes
23 answers

static constructors in C++? I need to initialize private static objects

I want to have a class with a private static data member (a vector that contains all the characters a-z). In java or C#, I can just make a "static constructor" that will run before I make any instances of the class, and sets up the static data…
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
187
votes
20 answers

What is the use of a private static variable in Java?

If a variable is declared as public static varName;, then I can access it from anywhere as ClassName.varName. I am also aware that static members are shared by all instances of a class and are not reallocated in each instance. Is declaring a…
Vaibhav Jani
  • 12,428
  • 10
  • 61
  • 73
176
votes
4 answers

Friend declaration in C++ - difference between public and private

Is there a difference between declaring a friend function/class as private or public? I can't seem to find anything about this online. I mean the difference between: class A { public: friend class B; }; and class A { private: //or nothing…
BIU
  • 2,340
  • 3
  • 17
  • 23
174
votes
6 answers

Any way to Invoke a private method?

I have a class that uses XML and reflection to return Objects to another class. Normally these objects are sub fields of an external object, but occasionally it's something I want to generate on the fly. I've tried something like this but to no…
Sheldon Ross
  • 5,364
  • 7
  • 31
  • 37
157
votes
10 answers

What is a good example to differentiate between fileprivate and private in Swift3

This article has been helpful in understanding the new access specifiers in Swift 3. It also gives some examples of different usages of fileprivate and private. My question is - isn't using fileprivate on a function that is going to be used only…
Nikita P
  • 4,226
  • 5
  • 31
  • 55
157
votes
11 answers

Unit testing of private functions with Mocha and Node.js

I am using Mocha in order to unit test an application written for Node.js. I wonder if it's possible to unit test functions that have not been exported in a module. Example: I have a lot of functions defined like this in foobar.js: function…
fstab
  • 4,801
  • 8
  • 34
  • 66
148
votes
6 answers

Is it possible to set private property via reflection?

Can I set a private property via reflection? public abstract class Entity { private int _id; private DateTime? _createdOn; public virtual T Id { get { return _id; } private set { ChangePropertyAndNotify(ref _id,…
AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152
146
votes
16 answers

What's the best way to unit test protected & private methods in Ruby?

What's the best way to unit test protected and private methods in Ruby, using the standard Ruby Test::Unit framework? I'm sure somebody will pipe up and dogmatically assert that "you should only unit test public methods; if it needs unit testing, it…
Brent Chapman
  • 2,519
  • 3
  • 20
  • 10
128
votes
10 answers

Any reason to write the "private" keyword in C#?

As far as I know, private is the default everywhere in C# (meaning that if I don't write public, protected, internal, etc. it will be private by default). (Please correct me if I am wrong.) So, what's the reason to write that keyword, or why does it…
Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
126
votes
10 answers

When should an attribute be private and made a read-only property?

I don't know when an attribute should be private and whether I should use property. I read recently that setters and getters are not pythonic but using the property decorator is OK. But what if I have attribute, that mustn't be set from outside of…
Rafał Łużyński
  • 7,083
  • 5
  • 26
  • 38
114
votes
2 answers

What are the differences between the private keyword and private fields in TypeScript?

In TypeScript 3.8+, what are the differences between using the private keyword to mark a member private: class PrivateKeywordClass { private value = 1; } And using the # private fields proposed for JavaScript: class PrivateFieldClass { …
Matt Bierner
  • 58,117
  • 21
  • 175
  • 206
105
votes
6 answers

Inheritance of private and protected methods in Python

I know, there are no 'real' private/protected methods in Python. This approach isn't meant to hide anything; I just want to understand what Python does. class Parent(object): def _protected(self): pass def __private(self): …
uloco
  • 2,283
  • 4
  • 22
  • 37
104
votes
13 answers

Private Constructor in Python

How do I create a private constructor which should be called only by the static function of the class and not from else where?
user1057793
  • 1,041
  • 2
  • 7
  • 3
104
votes
6 answers

Why can I access private variables in the copy constructor?

I have learned that I can never access a private variable, only with a get-function in the class. But then why can I access it in the copy constructor? Example: Field::Field(const Field& f) { pFirst = new T[f.capacity()]; pLast = pFirst +…
demonking
  • 2,584
  • 5
  • 23
  • 28