Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default Package access for class

    Hi all

    i have a doubt regarding class access modifier.
    i have read that class have default package access, and classes are visible in other classes in same package.

    i did code in two files but package same.
    but second file (which access class in first file) not get compiled....

    what was the reason?

    file1
    ------
    package Pack1;

    public class Test1
    {
    int x = 1;
    }


    File 2
    -------
    package Pack1;

    public class Test2
    {

    public static void main(String[] args)
    {
    Test1 objTest1 = new Test1();
    System.out.println(objTest1.x);
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    Hi all

    i have a doubt regarding class access modifier.
    i have read that class have default package access, and classes are visible in other classes in same package.

    i did code in two files but package same.
    but second file (which access class in first file) not get compiled....

    what was the reason?

    file1
    ------
    package Pack1;

    public class Test1
    {
    int x = 1;
    }


    File 2
    -------
    package Pack1;

    public class Test2
    {

    public static void main(String[] args)
    {
    Test1 objTest1 = new Test1();
    System.out.println(objTest1.x);
    }
    }
    Compile both your .java files at once (pass them both to javac on the command line). Javac doesn't know where your files are stored and it assumes nothing so it can't find your other .java file when it compiles one of them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks jos, but how i compile both files together in command prompt?

    one more thing...
    when i removed package statement from both files, it works fine

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    How are you compiling them?
    What's the exact command?
    What directory are you in when you compile them?

  5. #5
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    i placed both files in a directory named Pack1
    then i use command prompt and went to Pack1

    then javac Test1.java it ok
    then javac Test2.java it gets error

    but javac *.java works fine

  6. #6
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks jos

  7. #7
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    when i compiled both using javac *.java it worked
    but on running Test2 i get error
    java.lang.NoClassDefFoundError: Test2

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    thanks jos, but how i compile both files together in command prompt?
    Java Code:
    javac Test1.java Test2.java
    or:
    javac *.java
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jomypgeorge View Post
    when i compiled both using javac *.java it worked
    but on running Test2 i get error
    java.lang.NoClassDefFoundError: Test2
    Set the classpath on the command line of the java.exe command. Type "java -help" for the details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Yep.
    Thought so.
    As Jos says, classpath.

    ETA: When running you should be running from the root directory. That is the directory off which your packages(s) sit, so one up from the Pack1 directory. Then you would run the code as "java Pack1/Test2".

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Yep.
    Thought so.
    As Jos says, classpath.

    ETA: When running you should be running from the root directory. That is the directory off which your packages(s) sit, so one up from the Pack1 directory. Then you would run the code as "java Pack1/Test2".
    Completely off topic so shoot me, but what is 'ETA'?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Edited To Add.

    Or, if it's trains/planes/automobiles, Estimated Time of Arrival.
    :)

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Edited To Add.

    Or, if it's trains/planes/automobiles, Estimated Time of Arrival.
    :)
    Ah, ok, thanks; I knew that last meaning but that didn't make any sense (as most things don't in my life ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    I used to be confused by it as well, until someone explained it to me.

    So now I use it to confuse others...I like to share.
    :)

  15. #15
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks jos and tool
    it worked

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    "tool"?

    Should I be offended...?
    ;)

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    "tool"?

    Should I be offended...?
    ;)
    It's just another acronym, you just don't know the meaning of it; no need to worry or to feel offended.

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    heyyyy....
    sorry Tools, i typed it in a hurry....

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    It's two "l"s not two "o"s...:)

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    It's two "l"s not two "o"s...:)
    Tooolls?

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. why I get error while trying to run a class in a package
    By newlifeseattle in forum New To Java
    Replies: 4
    Last Post: 06-01-2010, 07:15 PM
  2. Replies: 0
    Last Post: 04-04-2010, 10:06 AM
  3. Replies: 1
    Last Post: 03-04-2010, 04:07 PM
  4. Default Access (package access) confusion
    By gauravrajbehl in forum New To Java
    Replies: 1
    Last Post: 11-18-2009, 10:48 AM
  5. Compiling a class in a package from cmd
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10:27 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
  •