• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

What is wrong with this program

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything time i try to compile it wouldn't and i do ot know what is wrong,please somone help me to correct it,compile and run it on my behalf.And finally i need to know where the mistake are.
//: c05:Lunch.java
// Demonstrates class access specifiers. Make a class
// effectively private with private constructors:

class Soup {
private Soup() {}
// (1) Allow creation via static method:
public static Soup makeSoup() {
return new Soup();
}
// (2) Create a static object and return a reference
// upon request.(The "Singleton" pattern):
private static Soup ps1 = new Soup();
public static Soup access() {
return ps1;
}
public void f() {}
}

class Sandwich { // Uses Lunch
void f() { new Lunch(); }
}

// Only one public class allowed per file:
public class Lunch {
void test() {
// Can't do this! Private constructor:
//! Soup priv1 = new Soup();
Soup priv2 = Soup.makeSoup();
Sandwich f1 = new Sandwich();
Soup.access().f();
}
} ///:~

This is the error it throws out
C:\java\java src\Soup.java:25: class Lunch is public, should be declared in a file named Lunch.java
public class Lunch {
^
1 error

Tool completed with exit code 1
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message tells you exactly what is wrong. it says "In your file Soup.java, you have a public class called Lunch. That public class must be declared in a file named Lunch.java".

In other words, the file name MUST EXACTLY MATCH the name of the one and only public class in that file. Since your public class is called "Lunch", your file MUST be named "Lunch.java", not "Soup.java".
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here's the thing to ALWAYS keep in mind.

1. If you have defined a public class say "public class xyz{}" and a default class "class abc{}", then you MUST save the file as xyz.java

2. If you do not have ANY public class, say "class abc{}" and "class xyz{}", then you can save the file with any name ex - qwerty.java or abc.java or xyz.java

Thanks,
Dinesh
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dinesh Arora:
...If you do not have ANY public class, say "class abc{}" and "class xyz{}", then you can save the file with any name ex - qwerty.java or abc.java or xyz.java...


That's correct. You can do this. But a year from now, when you're trying to find the source code for abc.class, you might wish you named things differently.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:

That's correct. You can do this. But a year from now, when you're trying to find the source code for abc.class, you might wish you named things differently.



Hehe, I agree, I always try to do that, I had bad experience when I had that bad habits years ago
[ June 06, 2007: Message edited by: Jenson Chew ]
 
Abbey Samuel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Guys i'm beginning to learn this language and i bet by the next 2-3 months,i should have gone beyond this stage.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Originally posted by Dinesh

...If you do not have ANY public class, say "class abc{}" and "class xyz{}", then you can save the file with any name ex - qwerty.java or abc.java or xyz.java...

its true because, the Java compiler will take care of the filenames and after sucessful compilation, it will create two SEPERATE class files
abc.class and xyz.class

but for a public class, it is must that the file name and the name of the class should be identical

Hope this helps
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic