Results 1 to 4 of 4
  1. #1
    DONALD is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default How is this java class usage valid ?

    Java Code:
    This is an example taken from Murachs Java textbook. The first code is a part of a Validator class:--- 
    
     import java.util.Scanner; 
    
     public class Validator 
     { 
             public static String getString(Scanner sc, String prompt) 
             { 
                  System.out.print(prompt); 
                  String s = sc.next(); // read user entry 
                  sc.nextLine(); // discard any other data entered on the line 
                  return s; 
             } 
        .................
        ................
    -----------------

    Instantiation of Scanner is not needed in this Validator class but is needed in the main application using this class. Why?
    What mechanism allows the compiler to permit this ? I am aware that Scanner is a Final class. Is this part of the reason ?
    Last edited by DONALD; 08-25-2015 at 03:28 AM. Reason: Reformat text

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How is this java class usage valid ?

    Quote Originally Posted by DONALD View Post
    [CODE]
    Instantiation of Scanner is not needed in this Validator class but is needed in the main application using this class. Why?
    What mechanism allows the compiler to permit this ? I am aware that Scanner is a Final class. Is this part of the reason ?
    In the case of the Scanner class, it is designated as final so it can't be subclassed. What is going on is that somewhere
    the getString method is called and an instance of the Scanner is passed to the method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DONALD is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: How is this java class usage valid ?

    My question relates to Netbeans IDE error checking.
    When I insert the Validator class code into the IDE editor, how does the error checking built into the IDE know that Class Validator's use of Scanner refers to a calling Main class that instantiates a new scanner object ? This is done even before the project is built.
    This is my observation with the testing I've done so far.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How is this java class usage valid ?

    Quote Originally Posted by DONALD View Post
    My question relates to Netbeans IDE error checking.
    When I insert the Validator class code into the IDE editor, how does the error checking built into the IDE know that Class Validator's use of Scanner refers to a calling Main class that instantiates a new scanner object ? This is done even before the project is built.
    This is my observation with the testing I've done so far.
    Not certain with NetBeans, but with Eclipse, every time a source file is edited, Eclipse does syntax checks just the way the compiler does (in fact, it probably uses the compiler to do this). This is a configurable option in the Eclipes IDE.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. How to check a method usage in class source code
    By Marion in forum Advanced Java
    Replies: 3
    Last Post: 04-08-2012, 01:33 PM
  2. valid java identifiers
    By kulangotski in forum New To Java
    Replies: 7
    Last Post: 01-09-2011, 09:47 PM
  3. a little help with reading in files, and class usage =/
    By basketball8533 in forum New To Java
    Replies: 10
    Last Post: 10-08-2010, 06:01 AM
  4. Replies: 1
    Last Post: 11-17-2008, 09:32 AM
  5. MySite/Valid.java:56: cannot resolve symbol
    By Albert in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-05-2007, 05:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •