Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Test Drive

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
VI Editor Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    Bartender
    Posts: 3225
    34
    IntelliJ IDE Oracle Spring Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Marshal
    Posts: 74067
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 3225
    34
    IntelliJ IDE Oracle Spring Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 74067
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Destroy anything that stands in your way. Except this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic