• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is wrong with this program

 
Abbey Samuel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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".
 
Dinesh Arora
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
Jenson Chew
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic