The private
method of a class can only be accessible inside the class. The private
methods cannot be called using the object outside of the class. If we try to access the private method outside the class, we'll get SyntaxError
.
Our private method can be declared by adding the #
before the method name.
Line 6: We have created a new class with the name ClassWithPrivateMethod
.
Lines 7–9: We declare a private method with the name #privateMethod
.
Lines 10–12: We declare a static private method with the name #privateStaticMethod
.
Lines 14–16: We declare a method with a name callPrivateMethod
that will call the #privateMethod
. The member
function of the class can access the private
method internally.
Lines 17–19: We declare a method with a name callPrivateStaticMethod
that will call the #privateStaticMethod
. The member
function of the class can access the private
static method internally.
Line 21: We create a new object obj
for the class ClassWithPrivateMethod
.
Line 22: We call the callPrivateMethod
method, which will internally call the #privateMethod
of the class.
Line 22: We call the callPrivateStaticMethod
method, which will internally call the #privateStaticMethod
of the class.