• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public declaration in methods

 
Greedy thomas
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Is the public keyword the regarded as a default when defining methods. I have a class that contains only methods that I wish to call from another file/class. I have methods as shown below :
int getClose() {
return this.closeTime ;
}

void setClose(int closeTime) {
this.closeTime = closeTime ;
}
This compiles and runs fine. But I have also seen that if you declare the methods as public int getClose() and public void setClose(int closeTime), it also works fine. The only difference is that in the latter I have added the keyword public to the method definition.
So, because both versions work fine, can I assume that the keyword public is a default definition.
Thanks In Advance,
Rajesh
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, public is not the default.
If you omit the access specifier, the default access (sometime referred to unimaginatively as 'default access', but also termed 'package protected') acts as if you specified the protected access specifier but also makes the element available to classes in the same package.
Non-extending classes outside the package will have no access to the element.
hth,
bear
[ June 08, 2003: Message edited by: Bear Bibeault ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you omit the access specifier, the default access (sometime referred to unimaginatively as 'default access', but also termed 'package protected') acts as if you specified the protected access specifier but also makes the element available to classes in the same package.
package private Bear. Classes outside the package do not have access not even subclasses which have access to all protected methods. Protected methods are also available to the whole package.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! Right. Brain fart!
wiping egg off face,
bear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic