Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Test Drive  RSS feed

 
Jackson Thurston
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading Head First: Java, I made it to the middle of chapter 5 (page 106) and one thing caught me off guard; Would I need to save the SimpleDotComTestDrive class in a different Java file, or put it in with the rest. Maybe it explained it, but I can't find it.
 
Stefaan Dutry
Ranch Hand
Posts: 33
Java Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a class is declared with the public modifier, it needs to be declared in a file with the same name as the class.
  • SimpleDotComTestDrive.java
  • SimpleDotCom.java
  •  
    Jackson Thurston
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you! But to run the test drive, what do I have to do? It obviously won't run on it's own since methods are within the other class. Would I have to put them in a .jar file?
     
    Mohamed Sanaulla
    Bartender
    Posts: 3179
    34
    Google App Engine Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So you can have multiple classes declared in a single java source file. but there can be only one public class in the file and the name of the file should be same as that of the public class followed by .java extension. Also note that the class having the public static void main(String[] args) should be declared as public and rest of the rules mentioned before apply.

    Jackson Thurston wrote:Thank you! But to run the test drive, what do I have to do? It obviously won't run on it's own since methods are within the other class. Would I have to put them in a .jar file?

    Its enough to have the classes you are using in your classpath- Read the article- HowToSetTheClasspath To know how to make use of the classpath.
     
    Campbell Ritchie
    Marshal
    Posts: 55678
    161
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mohamed Sanaulla wrote: . . . the class having the public static void main(String[] args) should be declared as public
    Why? You can invoke the main method in a package-private class, particularly if, as here, you are using the unnamed package
    Mohamed Sanaulla wrote: . . . Its enough to have the classes you are using in your classpath-. . . .
    At this stage it is easier to put all the classes in the same directory and use that as the unnamed package. Then you don't need to set a classpath.
     
    Mohamed Sanaulla
    Bartender
    Posts: 3179
    34
    Google App Engine Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Mohamed Sanaulla wrote: . . . the class having the public static void main(String[] args) should be declared as public
    Why? You can invoke the main method in a package-private class, particularly if, as here, you are using the unnamed package

    Oh yeah really not necessary
    Campbell Ritchie wrote:
    Mohamed Sanaulla wrote: . . . Its enough to have the classes you are using in your classpath-. . . .
    At this stage it is easier to put all the classes in the same directory and use that as the unnamed package. Then you don't need to set a classpath.


    That's how I would start learning. But then thought of sharing just for the information of OP
     
    Campbell Ritchie
    Marshal
    Posts: 55678
    161
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mohamed Sanaulla wrote: . . . That's how I would start learning. But then thought of sharing just for the information of OP


    It makes for more interesting discussion
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!