• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Classes and objects error

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot figure out what is wrong with my code. Just doing some example codes from my textbook and copying word for word as much as I can




Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at MovieTestDrive.main(Movie.java:14)

 
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ryan Hickman!
When you post code in your post please use code button like this:



Solution:

You can declare only one public class in one java file. Remove public specifier of Movie class.
When I ran the code on my machine after removing the public specifier of Movie class, it results in "Playing the movie".
 
Ryan Hickman
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you much!
 
Marshal
Posts: 74376
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aniket S. Kulkarni wrote: . . . You can declare only one public class in one java file. Remove public specifier of Movie class. . . .

Disagree. You might do better to move the Movie class into a file of its own, then it can still be public.
And don’t say specifier; it is called a modifier.
 
Ryan Hickman
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't that what I have already done?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Hickman wrote:Isn't that what I have already done?



Hard to tell from what you posted. Is that the contents of one file or two?

You should have one file called Movie.java that contains the Movie class and a separate file called MovieTestDrive.java that contains the MovieTestDrive class.
 
Ryan Hickman
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is one file. There is one class above and a tester class below with objects, variables, and methods.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Hickman wrote:It is one file. There is one class above and a tester class below with objects, variables, and methods.



Then, to answer your question, no you haven't already done that. That is, you haven't followed the advice to "move the Movie class into a file of its own."
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry! Campbell Ritchie.
I must call it as "Access Specifier" or "modifier". You are right.

If you mark two classes as public inside one java file then it results in an error as follows:

MovieTestDrive.java:1: error: class Movie is public, should be declared in a file named Movie.java
public class Movie{
^
1 error


I want to say, you cannot mark more than one class as public inside one java file. Yes of course you can place Movie class in Movie.java file, but in same file you can not mark it as public.

Don't misinterpret. Am I correct or not?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aniket S. Kulkarni wrote:
I want to say, you cannot mark more than one class as public inside one java file. Yes of course you can place Movie class in Movie.java file, but in same file you can not mark it as public.

Don't misinterpret. Am I correct or not?



You're close.

You can't have two top-level public classes in the same .class file. You can have a a top-level public class and one or more nested public classes though.

Also (although this is a really minor point), I think the spec allows multiple top-level public classes in the same source file, just not in the same class file. Compilers are free to enforce tighter restrictions though, and every one that I know of these days does prohibit multiple top-level public classes in the same .java file. (About 12-14 years ago, I think I used a compiler from IBM or someplace that didn't enforce that restriction on .java files.)
 
Campbell Ritchie
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I meant was that I thought it a better solution to create two files than take the modifier off Movie.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic