• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Package not working with object creation in simple program

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, my simple program is not working. I created the package with the Student file within by using javac -d . Student.java after compiling it. Then, the ThisDemo class does not compile. I did some debugging and found issues, but still getting the exact same errors.





Just to reiterate- Student.java compiled fine, then I made test1 package with Student inside of it. After that, well, nothing works.

EDIT for console error:


The student not having main when being run was my mistake since it has no main, and the I forgot brackets for ThisDemo after args- but the other errors baffle me.
 
Master Rancher
Posts: 4194
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

getting the exact same errors.


Please copy the full text of the error messages and paste it here so we can see what problems you are having.
 
William Golovlev
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

getting the exact same errors.


Please copy the full text of the error messages and paste it here so we can see what problems you are having.



whoops, edited.
 
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Golovlev wrote:. . . whoops, edited.

Please don't edit posts like that; it makes it appear that Norm was mistaken.

The error message you showed proves that the package structure isn't the problem. It is that the constructor you are calling has package‑private (=default) access; it is only accessible from the main() method if you give it public access.

Please only use _ in identifiers in,
  • 1: Package names, as described in the Java™ Tutorials, and
  • 2: Constant names ALL_IN_UPPER_CASE.
  • Other identifiers should be written inMixedCase.
     
    William Golovlev
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    My professor put up code that purposefully is wrong, so there are errors I am still working with. I forgot a constructor must be declared as public, but after putting the public access modifier, I Still get this after saving, deleting the package and recompiling it-

     
    Norm Radder
    Master Rancher
    Posts: 4194
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    is not public in Student


    Check that the Student class's constructor is public.  Make sure there are not old versions around.
     
    Saloon Keeper
    Posts: 23282
    158
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Your file locations are wrong. Java package/directory rules require that the compiler should output file test1/Student.class Preferably the compile source would be file test1/Student.java.

    Also, your error message comes from the fact (as Campbell said) that you did not declare the constructor for Student to be public, so it defaulted to package scope. One of Java's quirkier features.
     
    Campbell Ritchie
    Marshal
    Posts: 72088
    312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tim Holloway wrote:. . . . One of Java's quirkier features.

    There are worse features. Some of the worst have been deprecated.
     
    Campbell Ritchie
    Marshal
    Posts: 72088
    312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    William Golovlev wrote:. . . a constructor must be declared as public

    That isn't correct. A constructor or class member must be public in order to be accessible from different classes or objects outside the package. You need such access.

    My professor put up code that purposefully is wrong, so there are errors I am still working with. . . .

    Does that include using a char for status?The compiler won't notice, but I did.

    Please don't quote the whole of the preceding post; such quotes are liable to removal without notice.
     
    William Golovlev
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok well I am not quite sure why this is an issue because I have made my file contain a public constructor..



    Not sure if file locations are causing the issue, I have a mywork folder with all my work, then Student is in test1 folder (package) within the mywork folder.

     
    Norm Radder
    Master Rancher
    Posts: 4194
    38
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Are there any old versions of the Student class file around?
     
    Campbell Ritchie
    Marshal
    Posts: 72088
    312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ...and please show us the directory structure you are working in. There is something we don't know about yet.
     
    William Golovlev
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    William Golovlev wrote:Ok well I am not quite sure why this is an issue because I have made my file contain a public constructor..



    Update: never was an issue with the code. For some reason I was compiling, and everytime I compiled it was not re-compiling the file in my package folder. Thought I was doing it right with "java -d . ThisDemo.java" but deleting all class files and recompiling worked. Thanks!
     
    Campbell Ritchie
    Marshal
    Posts: 72088
    312
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done and thank you for explaining the solution.
     
    New rule: no elephants at the chess tournament. Tiny ads are still okay.
    the value of filler advertising in 2020
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic