Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about access modifier (Java OCA 8 Programmer I Study Guide)

 
Martin Müller
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello from Germany,



Doesn't the AccessModifier of the above variable in the Egg class, Ch. 1, p. 20 have to be public? Otherwise the main class can't directly print it and a getNumber() method would be needed.

-Martin
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin Müller,

First of all, a warm welcome to CodeRanch!

Martin Müller wrote:Doesn't the AccessModifier of the above variable in the Egg class, Ch. 1, p. 20 have to be public? Otherwise the main class can't directly print it and a getNumber() method would be needed.

No, that's not required because the main method is defined in the Egg class as well. And every method defined in a class can access other private members (variables and methods). It would be true if the main method (accessing the number variable) was defined in another class (e.g. TestEgg).

Let me share the complete code snippet so everyone can join this discussion

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic