• Post Reply Bookmark Topic Watch Topic
  • New Topic

why public came to picture  RSS feed

 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why should i declare public




all are implied to be public.... in that case why should i declare public ..... and

will be called anyway wen .class file is loaded what is the use of public ??? ......


 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun:

Just because a class is public doesn't make its methods public automatically. Also, methods are package private by default, not public.

John.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyway ....can make things going write ......class file will be loaded.... static things ..can come into existence.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun:

There's a difference between the 'package private' and 'private' access levels. The 'package private' level is the default, and it will be set if you do not specify another access level. Package private methods and members are visible to other classes in the same package. The 'private' access level is set when you define a method or member using the 'private' keyword. Private methods and members are visible only to the instance or class, in the case of static methods or members.

John.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,what's with package private ...... why should JVM bother on it ... in the FIRST PLACE . on executing it.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:hey,what's with package private ...... why should JVM bother on it ... in the FIRST PLACE . on executing it.
That response is not of the standard of politeness we like to see here.
 
lakmal padmakumara
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you change

to

you have said GoodBye to your main method. That's another static method which only allows to be use inside the same package.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For more information on access modifiers (public, protected, private), what they mean exactly and when to use what, have a look at Controlling Access to Members of a Class in The Java Tutorials.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!