This discussion is archived
1 2 Previous Next 28 Replies Latest reply: Aug 20, 2013 12:58 AM by 9423755 RSS

Why does this not work?

9423755 Explorer
Currently Being Moderated

test\Test7.java:

 

package test;

public class Test7

{  

    public static void foo(int x)

    {

        System.out.println("test 7 foo int:" + x);

    }

}

 

Test8.java:

 

public class Test8

{

    public static void main(String[] args)

    {

        System.out.println("Test8 main");

        test.Test7.foo(10);       

    }

}

 

command line:

 

C:\Users\J\My Documents\java>dir Test8.*

Directory of C:\Users\J\My Documents\java

13/08/2013  10:42               220 Test8.java

               1 File(s)            220 bytes

               0 Dir(s)  35,856,887,808 bytes free

 

 

C:\Users\J\My Documents\java>dir test\Test7*

Directory of C:\Users\J\My Documents\java\test

13/08/2013  10:03               147 Test7.java

               1 File(s)            147 bytes

               0 Dir(s)  35,856,887,808 bytes free

 

 

C:\Users\J\My Documents\java>javac Test8.java

Test8.java:8: error: cannot find symbol

        test.Test7.foo(10);

            ^

  symbol:   variable Test7

  location: class test

1 error

 

 

 

Changing Test8.java so that it uses an import statement allows compilation to succeed:

 

import test.Test7;

public class Test8

{

    public static void main(String[] args)

    {

        System.out.println("Test8 main");

       Test7.foo(10);       

    }

}

 

C:\Users\J\My Documents\java>javac Test8.java

 

C:\Users\J\My Documents\java>

 

 

Why?

  • 1. Re: Why does this not work?
    rp0428 Guru
    Currently Being Moderated

    Because the package 'test' is NOT observable in class 'Test8. Yet another reason why you should ALWAYS put your classes ('Test8') in a package.

     

    See the Java Language Spec

    http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.4.3

    6.3. Scope of a Declaration

    The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name, provided it is visible (§6.4.1).

    A declaration is said to be in scope at a particular point in a program if and only if the declaration's scope includes that point.

    The scope of the declaration of an observable (§7.4.3) top level package is all observable compilation units (§7.3).

    The declaration of a package that is not observable is never in scope.

    7.4.3. Observability of a Package


    A package is observable if and only if either:

    •   A compilation unit containing a declaration of the package is observable (§7.3).
    •   A subpackage of the package is observable.

    The packages java, java.lang, and java.io are always observable.

  • 2. Re: Why does this not work?
    9423755 Explorer
    Currently Being Moderated

    Thanks, but I'm still confused.

    From what you say, a package is not observable until I import it. Therefore java.util is not observable unless I import it.

    But of course I can do this:

     

    public class Test12

    {

        public static void main(String[] args)

        {

            java.util.ArrayList<String> a = new java.util.ArrayList<String>(); // fully-qualified use

        }

    }

     

    which compiles, no problem.

    So, why can I refer to a class within the package java.util WITHOUT importing the java.util package, but not refer to a class in my own package test, WITHOUT importing the package test?  What's the difference?

     

     

    Thanks.

  • 3. Re: Why does this not work?
    rp0428 Guru
    Currently Being Moderated

    So, why can I refer to a class within the package java.util WITHOUT importing the java.util package, but not refer to a class in my own package test, WITHOUT importing the package test?  What's the difference

    It is really hard to help someone that won't read the answers that they are given. Did you not read this in my reply?

    The packages java, java.lang, and java.io are always observable

    .

  • 5. Re: Why does this not work?
    9423755 Explorer
    Currently Being Moderated

    gimbal2 wrote:

     

    Note that this is a cross-post:

     

    Can't compile a class that invokes a method in a class in a different package

     

    What point are you trying to make gimbal? Who should note, and why?

  • 6. Re: Why does this not work?
    masijade Explorer
    Currently Being Moderated

    Everyone answering should "note" it, as they are most likely wasting their time providing answers you've already received elsewhere.

  • 7. Re: Why does this not work?
    9423755 Explorer
    Currently Being Moderated

    rp0428 wrote:

     

    So, why can I refer to a class within the package java.util WITHOUT importing the java.util package, but not refer to a class in my own package test, WITHOUT importing the package test?  What's the difference

    It is really hard to help someone that won't read the answers that they are given. Did you not read this in my reply?

    The packages java, java.lang, and java.io are always observable

    .

     

    What has that got to do with it? I'm using the java.util package in my counter-example.

    I can provide a million others. Here's one that uses the java.nio package:

     

    public class foo {

        public static void main(String[] args) {       

            java.nio.file.Path file = java.nio.file.Paths.get(System.getProperty("user.dir")).resolve("x").resolve("x.bin");

            System.out.println(file.getFileName().toString());

        }

    }  

     

    It uses a class in the java.nio package. It compiles. I don't need to use an import statement.

     

    So again, why can I compile this class which uses a class in a package NOT imported, and NOT compile my other class, which seems to require the class in package test TO BE imported?

     

    It is interesting that nobody seems to be able (willing perhaps) to answer this, which must surely be a trivial question for most java programmers.

  • 8. Re: Why does this not work?
    9423755 Explorer
    Currently Being Moderated

    masijade wrote:

     

    Everyone answering should "note" it, as they are most likely wasting their time providing answers you've already received elsewhere.

    MOST LIKELY?  Have you bothered to read the other thread, or are you making LAZY ASSUMPTIONS?

     

    Do you think I would go to the effort of posting in two places if I had already received an answer to my question?  What are you, stupid?

    I posted a question on a forum, and DID NOT receive an answer that explained what the cause of the problem is. I received answers, but none of them is satisfactory.

    Am I supposed to leave it at that?  Am I, uh, NOT ALLOWED to make a forum posting in two separate forums?  Oh, I'm sorry. What are you, some kind of global forum enforcement officer?

     

    If you can answer the question, answer the question. If you can't, you know where you can go.

  • 9. Re: Why does this not work?
    masijade Explorer
    Currently Being Moderated

    Your case is simple.  Your problem is that you are using the default package and the "test" package is NOT observable from your class.  What is so hard to understand about that.  And that fact makes it HIGHLY likely that you have already been told that using the default package is your problem without any need to look at the other thread (which that "most likely" points out or did you choose to ignore that, or are YOU just too "STUPID" to know what that means).  And I WOULD have looked at the other thread had you bothered to notify anyone that it existed.  NOT doing so is EXTREMELY rude, as is your response.  Yet another loser to place on the ignore list.

     

    Edit:  And I'll bet you STILL haven't notified anyone THERE that you've cross-posted this (although someone else has probably already done so) so anyone answering there would ALSO be wasting THEIR time.  And I'll bet you STILL don't understand just how rude that is, do you?

     

    Edit:  And besides, whether a package is observable or not do NOT use the default package, it only leads to problems.

  • 10. Re: Why does this not work?
    9423755 Explorer
    Currently Being Moderated

    masijade wrote:

     

    Your case is simple.  Your problem is that you are using the default package and the "test" package is NOT observable from your class.  What is so hard to understand about that.  And that fact makes it HIGHLY likely that you have already been told that using the default package is your problem without any need to look at the other thread (which that "most likely" points out or did you choose to ignore that, or are YOU just too "STUPID" to know what that means).  And I WOULD have looked at the other thread had you bothered to notify anyone that it existed.  NOT doing so is EXTREMELY rude, as is your response.  Yet another loser to place on the ignore list.

     

    "Highly likely"?  Again, you are making ASSUMPTIONS. Check your assumptions.

    You have NOT answered the question.

    In the first example, which does NOT compile without an import statement, I'm using the default package. The "test" package is NOT observable.

    In the counter-example, which DOES compile without an import statement, I'm using the java.util package. The java.util package is NOT observable. And yet it compiles, WITHOUT an import statement. 

    Do you see the issue?

     

    You have it would seem failed to grasp the nature of the problem.

    Let me make it crystal clear for you.

     

    Why does this compile:

     

    import test.Test7;

    public class Test8

    {

        public static void main(String[] args)

        {

            System.out.println("Test8 main");

            Test7.foo(10);      

        }

    }

     

    but this - WHICH REFERENCES A CLASS IN A "NON-OBSERVABLE PACKAGE WITHOUT AN IMPORT STATEMENT" - does not compile:

     

    public class Test8

    {

        public static void main(String[] args)

        {

            System.out.println("Test8 main");

            test.Test7.foo(10);      

        }

    }

     

    and yet this - WHICH REFERENCES A CLASS IN A "NON-OBSERVABLE" PACKAGE WITHOUT AN IMPORT STATEMENT - does compile:

     

    public class foo {

        public static void main(String[] args) {       

            java.nio.file.Path file = java.nio.file.Paths.get(System.getProperty("user.dir")).resolve("x").resolve("x.bin");

            System.out.println(file.getFileName().toString());

        }

    }  

     

    And by the way I'm under no obligation to tell you what I'm posting and where. The fact that you find it "extremely rude" that I'm not keeping you updated with my postings on the interweb says a lot about you. 

  • 11. Re: Why does this not work?
    masijade Explorer
    Currently Being Moderated

    Who cares whether assumptions are made or not.  The fact remains you have asked this question in two different places without letting them know that that is the case, so people asking on one site or the other, and many times both, WILL be wasting their time.  And I couldn't care less about observable or not.  Your MAIN problem is using the default package, for a myriad of reasons, whether or not it plays an influence in this one.  Have you even TRIED to give that class a package, yet?  Again, I will ASSUME not, as you have already proven yourself rude enough to ignore any answer that does not conform to any preconceived notions you already have.

     

    Edit:

    Jason_942375 wrote:

     

    And by the way I'm under no obligation to tell you what I'm posting and where. The fact that you find it "extremely rude" that I'm not keeping you updated with my postings on the interweb says a lot about you.

    Actually, COMMON COURTESY says you ARE.  You are, after all, the one who wants help, and wasting the time of those people who can give it is NOT the most courteous way of going about it, and the fact that you REFUSE to acknowledge that says a lot about YOU, and YOUR feelings of self-entitlement.

  • 12. Re: Why does this not work?
    9423755 Explorer
    Currently Being Moderated

    You're having a laugh. Really, you make so many unchecked and largely false assumptions. But hey, maybe you do that for a living. If it works for you, then it works for you.

     

    But let's leave that aside.

     

    "Who cares about observable"?

     

    I don't. It was something raised by Mr rp0428 above, quoting from the JLS.

     

    Telling me to put my code into a package DOES NOT explain the discrepancy.

    Jesus, how can I make it any clearer?  Isn't it clear to you what the issue is?

     

    NONE OF MY EXAMPLES USES A PACKAGE but only ONE of them does not compile. The question is, why?

     

    The question is NOT, "how can I make my code that doesn't compile, compile?"

     

    How can I make it any clearer? I don't think I can. If you don't understand, you don't understand.

  • 13. Re: Why does this not work?
    masijade Explorer
    Currently Being Moderated

    And as I said, have you even TRIED, yet.  But I'll take your answer as a RESOUNDING no.

     

    Jason_942375 wrote:

     

    NONE OF MY EXAMPLES USES A PACKAGE but only ONE of them does not compile. The question is, why?

     

    And you don't see that as a problem?  There is a REASON why this is STRONGLY discouraged in the specs.

  • 14. Re: Why does this not work?
    9423755 Explorer
    Currently Being Moderated

    masijade wrote:

     

    Who cares whether assumptions are made or not.  The fact remains you have asked this question in two different places without letting them know that that is the case, so people asking on one site or the other, and many times both, WILL be wasting their time.  And I couldn't care less about observable or not.  Your MAIN problem is using the default package, for a myriad of reasons, whether or not it plays an influence in this one.  Have you even TRIED to give that class a package, yet?  Again, I will ASSUME not, as you have already proven yourself rude enough to ignore any answer that does not conform to any preconceived notions you already have.

     

    Edit:

    Jason_942375 wrote:

     

    And by the way I'm under no obligation to tell you what I'm posting and where. The fact that you find it "extremely rude" that I'm not keeping you updated with my postings on the interweb says a lot about you.

    Actually, COMMON COURTESY says you ARE.  You are, after all, the one who wants help, and wasting the time of those people who can give it is NOT the most courteous way of going about it, and the fact that you REFUSE to acknowledge that says a lot about YOU, and YOUR feelings of self-entitlement.

     

    So let me get this right.

     

    I post a question on forum X. The question goes unanswered - none of the replies explains the cause of the problem.  No more replies are forthcoming.

    I then think, maybe I can try another forum. But ah, no, I'm not SUPPOSED to do that. Because to do so would be "WASTING" people's time!?  Even though the first forum didn't answer the question?  And to even have the temerity to think that I can TRY another forum is a sign of my, gasp, SELF-ENTITLEMENT?

     

    Jesus. H. Christ.  Speechless.

1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points