• Post Reply Bookmark Topic Watch Topic
  • New Topic

How could this be possible  RSS feed

 
Ara Tatous
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code, how can we call private methods of the class?




[HENRY: Added ciode tags]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A private method is private to the class -- it can only be called from code from that class.

Henry
 
Ara Tatous
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry,

But, these are private instance methods, and not static methods. If we had this.test()or just test(), it was a normal case. But, you're instantiating a new class object, and you become client of that class. So the this reference is different from the reference that you just instantiate and assigned the object to.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ara Tatous wrote:
But, these are private instance methods, and not static methods.



A private method is private to the class -- regardless of whether the method is a private static method or a private instance method. That is how it is defined by the Java Language Specification.

Henry

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little more detail from the specification...

A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.


The scope of private is for the body of the top level class. This means that a outer class can access the private members of its inner class, the inner class call access the private members of its outer class, and heck, an inner class can access the private members of another inner class within the same outer class. Think of "private" as being public to everything within the body of the top level outer class.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!