Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Static method access a private method of the same class?

Tags:

I have this question because of the singleton/named constructor. In both cases, the real constructors are protected or private, neither of which can be accessed from outside.

For example, a short named constructor is this:

 class A {   public:     static A createA() { return A(0); } // named constructor   private:     A (int x); }; int main(void) {    A a = A::createA();  } 

I thought static method can only access static data member, or access private data/method via an existing object. However, in the above code, private constructor A() isn't static, and at the time it is being called, no object exists either. So the only explanation I can think of is that static method can access non-static private method of the same class. Can anyone please either affirm or negate my thought, possibly with some lines of explanations?

I apologize if this is too trivial however the key words are too common and I wasn't able to find an answer in dozens of google pages. Thanks in advance.

like image 232
Flowing Cloud Avatar asked Aug 25 '16 12:08

Flowing Cloud


People also ask

Can static methods access private methods?

Yes, we can have private methods or private static methods in an interface in Java 9. We can use these methods to remove the code redundancy. Private methods can be useful or accessible only within that interface only. We can't access or inherit private methods from one interface to another interface or class.

Can static methods access private variables Java?

Non static private variable are only accessible in object level context/methods. Static method exist at class level .. so they do not allow usage of any instance variable directly (without attaching object reference to it).

Can you access a private non static class field with a public static method of the same class?

You can even access a nested static class from a non-static method, it absolutely fine.

Can private members be static?

How to initialize private static members in C++? Here we will see how to initialize the private static member variables initialization in C++. We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class.


2 Answers

A static member function has the same access rights as a non static member function. So yes, it can access any public, protected, and private variable in the class. However you need to pass an instance of the class to the function for the function to be able to access the member. Otherwise a static function can only directly access any other static member in the class.

like image 50
NathanOliver Avatar answered Sep 19 '22 19:09

NathanOliver


According to the standard §11/p2 Member access control [class.access] (Emphasis Mine):

A member of a class can also access all the names to which the class has access. A local class of a member function may access the same names that the member function itself may access.113

113) Access permissions are thus transitive and cumulative to nested and local classes.

Since a static member function is a member of a class it has access to all the names to which the class has access and consequently to the constructor of the class itself.

Consequently, in your example:

class A {   A(int x);   public:   static A createA() { return A(0); } // named constructor   }; 

static member function A::createA() has access to call private constructor A::A(int).

like image 34
101010 Avatar answered Sep 21 '22 19:09

101010