Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Tolls

Thread: A non-public class in a source file doesn't matching its name

  1. #1
    Quentin's Avatar
    Quentin is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default A non-public class in a source file doesn't matching its name

    I'm reading Core Java 9th edition and have a question about the following snippet concerning the steps the javac compiler follows to compile a program:

    [...]at first, searching a class within a package is discussed if the latter doesn't contain a full package name[...]

    It is a compile-time error if more than one class is found. (Classes must be unique, so the order of the import statements doesn't matter.)

    The compiler goes one step further. It looks at the source files to see if the source is newer than the class file. If so, the source file is recompiled automatically. Recall that you can import only public classes from other packages. A source file can only contain one public class, and the names of the file and the public class must match. Therefore, the compiler can easily locate source files for public classes. However, you can import nonpublic classes from the current package. These classes may be defined in source files with different names. If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class.
    I don't quite understand the red fragment. I wondered if the word "import" nonpublic classes from the current package weren't a synonym for the word "use", since why would we want to import a class from the same package when compiler searches the current package automatically anyway?

    However I wanted to test nonpublic classes that are contained in source file which name doesn't match the class name:

    NonpublicClass.java:
    Java Code:
    package com.work.company;
    
    class NonpublicClass
    {
    	public void description()
    	{
    		System.out.println("Working!");
    	}
    }
    CompanyClass.java:
    Java Code:
    package com.work.company;
    
    public class CompanyClass
    {
    	public void method()
    	{
    		NonpublicClass v = new NonpublicClass();
    		v.description();
    	}
    }
    Start.java:
    Java Code:
    package com.home;
    
    public class Start
    {
    	public static void main(String[] args)
    	{
    		com.work.company.CompanyClass n = new com.work.company.CompanyClass();
    		
    		n.method();
    	}
    }
    Everything's fine when the source file names are the same as above. However, when I change NonpublicClass.java to a different name, there's an error "cannot find symbol" in:

    Java Code:
    NonpublicClass v = new NonpublicClass();
    I noticed that the class file for NonpublicClass isn't even generated so that's probably the cause. If I change to the directory of the package the NonpublicClass is contained in and compile it directly, i.e. issue for example:

    Java Code:
    javac NonpublicClass_different_name.java
    then a proper class file is generated with the class name, that is NonpublicClass.class, and afterwards I can compile the main program in Start.java.

    So the question is what am I missing regarding the cited quote that reads:

    If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class.
    ? Because adding such an import to CompanyClass.java:

    import com.work.company.NonpublicClass;
    didn't help either...

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: A non-public class in a source file doesn't matching its name

    One thing I can think of is that you may need to actually define a public class with a matching name in the source file containing the NonpublicClass; I can't really see in your description that you did that, you only renamed the source file. It is perfectly valid to have multiple non-public classes in the same source file, but you must have a public class in there too.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: A non-public class in a source file doesn't matching its name

    Quote Originally Posted by gimbal2 View Post
    It is perfectly valid to have multiple non-public classes in the same source file, but you must have a public class in there too.
    Not the case.
    It's perfectly valid to have no public classes defined in a source file.
    It's also perfectly valid to have no class in the file matching the classname.

    Not sure what the OP has done, but these three classes work together -
    NotPublic.java
    Java Code:
    package stuff.pack2;
    
    class NotPublicFoo {
        @Override
        public String toString() {
            return "Woot!";
        }
    }
    SomeClass.java
    Java Code:
    package stuff.pack2;
    
    public class SomeClass {
        private NotPublicFoo notPublic = new NotPublicFoo();
    
        public void doSomething() {
            System.out.println(notPublic);
        }
    }
    TestThings.java
    Java Code:
    package stuff;
    
    public class TestThings {
        public static void main(String[] args) throws Exception {
            SubClass sc = new SubClass();
            sc.doSomething();
        }
    Which results in no errors and "Woot!" being printed out.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: A non-public class in a source file doesn't matching its name

    Really :s Jebus Java still allows too much IMO.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Quentin's Avatar
    Quentin is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: A non-public class in a source file doesn't matching its name

    Quote Originally Posted by Tolls View Post
    Not sure what the OP has done, but these three classes work together
    I made a directory Base2, included stuff and stuff\pack2 directories in it, copied all of your files into them accordingly and got the same errors as before:

    C:\Users\Quent_000\Desktop\Base2>javac stuff\TestThings.java
    stuff\TestThings.java:5: error: cannot find symbol
    SubClass sc = new SubClass();
    ^
    symbol: class SubClass
    location: class TestThings
    stuff\TestThings.java:5: error: cannot find symbol
    SubClass sc = new SubClass();
    ^
    symbol: class SubClass
    location: class TestThings
    2 errors

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

    Default Re: A non-public class in a source file doesn't matching its name

    First, SubClass doesn't exist in the files Tolls gave you. I think he meant SomeClass. Second, first you
    need to compile the dependencies specifying their full path. Then when you run TestThings, use stuff.TestThings
    as the argument to java.

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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: A non-public class in a source file doesn't matching its name

    Re: SubClass/SomeClass.
    Whoops.
    I changed the name as I was hijacking some other test code of mine.
    Missed a bit!

    But yes, because NotPublicFoo does not have the same name as the name of its source file you have to manually compile that one.
    The compiler cannot find it under its own steam.
    Once it's compiled then it will find the .class file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Quentin's Avatar
    Quentin is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: A non-public class in a source file doesn't matching its name

    Quote Originally Posted by Tolls View Post
    But yes, because NotPublicFoo does not have the same name as the name of its source file you have to manually compile that one.
    The compiler cannot find it under its own steam.
    Once it's compiled then it will find the .class file.
    Exactly, but as I've pointed in the first post, the book reads:

    If you import a class from the current package, the compiler searches all source files of the current package to see which one defines the class.
    It's wrong then?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: A non-public class in a source file doesn't matching its name

    Looks like it.
    Just tried with 1.7, and it failed (not too surprisingly, frankly) to find the class.
    I used IntelliJ before, which auto compiles everything.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 09:17 AM
  2. compiler telling me to make a file for a public class
    By silverglade in forum New To Java
    Replies: 10
    Last Post: 04-26-2011, 07:47 AM
  3. How can I import a class file to my source ?
    By TheTypicalStudent in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 05:12 AM
  4. Replies: 1
    Last Post: 10-08-2010, 02:19 AM
  5. Replies: 5
    Last Post: 06-26-2009, 03:03 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
  •