This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP Chapter 1 - Declarations and Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP Chapter 1 - Declarations and Access" Watch "SCJP Chapter 1 - Declarations and Access" New topic
Author

SCJP Chapter 1 - Declarations and Access

R. Warren
Greenhorn

Joined: Oct 03, 2012
Posts: 3
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 18, 2010
Posts: 30


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

Joined: Jun 01, 2010
Posts: 47

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.

Warm Regards,
Rakesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP Chapter 1 - Declarations and Access
 
Similar Threads
Is default level class access really package level access?
Two Packages in the Same Directory?
Default access modifier. What does it do?
ClassNames
Here is some food for thought in Java :-)