• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP Chapter 1 - Declarations and Access

 
R. Warren
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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...
 
varun Dosapati
Ranch Hand
Posts: 31
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic