Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of Private Constructor in Thread Example

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Head First Java, Page 526, typed this up verbatim:



Why is the compiler outputting:



TestThreads.java:32: getAccum() has private access in Accum
Accum a = Accum.getAccum();
^
TestThreads.java:46: getAccum() has private access in Accum
Accum a = Accum.getAccum();
^
2 errors



Please explain
 
Suresh Babu Venkat
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats because the method is private and we cannot access methods which are private from anywhere other than with in the same class
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandra Bachan wrote:
Why is the compiler outputting:
TestThreads.java:32: getAccum() has private access in Accum
Accum a = Accum.getAccum();
^
TestThreads.java:46: getAccum() has private access in Accum
Accum a = Accum.getAccum();
^
2 errors

Please explain

Since the method getAccum() in the Accum class has the private access, you can't access it beyond that class. That's why you get Compilation Error! change it to public, then it will Compile perfectly!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic