• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP Chapter 1 - Declarations and Access

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just finished studying chapter 1 and I would like the following questions answered in order to further clarify the topic.

1. If I have 2 source files in the same directory, and 1 source file has a package statement and 1 source file does not have a package statement - Are these source code files in the same package?

2. If you declare a class with package access level, why would you ever mark any of it's members as public?
(I'm assuming that this would never be done, but would like someone to confirm this.)

3. Would you ever have a source file without a package statement (making it a default package) containing a public class - that would be used for anything other than a small test program? After all, a default package can't be imported.

This is my first post, so I hope it follows protocol.

Thanks.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Warren wrote:
1. If I have 2 source files in the same directory, and 1 source file has a package statement and 1 source file does not have a package statement - Are these source code files in the same package?



remember javac wont complaint. because it designed to check java grammar...
 
Ranch Hand
Posts: 31
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

3. Would you ever have a source file without a package statement (making it a default package) containing a public class - that would be used for anything other than a small test program? After all, a default package can't be imported.

Yes you are right

Just an example




Try to compile and execute the code So it will be more helpful
 
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Warren wrote:
1. If I have 2 source files in the same directory, and 1 source file has a package statement and 1 source file does not have a package statement - Are these source code files in the same package?



No, they are not in the same package.


R. Warren wrote:
2. If you declare a class with package access level, why would you ever mark any of it's members as public?
(I'm assuming that this would never be done, but would like someone to confirm this.)



I dont think that happens normally when it comes to production level code. By having a class level package you are restricting the access there itself. So, no point in having public members.


R. Warren wrote:
3. Would you ever have a source file without a package statement (making it a default package) containing a public class - that would be used for anything other than a small test program? After all, a default package can't be imported.



in lines with what varun said, classes with default package wont be used in production code.

Hope that helps.

- Rakesh K.
 
I don't even know how to spell CIA. But this tiny ad does:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic