aspose file tools*
The moose likes Beginning Java and the fly likes What is wrong with this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is wrong with this program" Watch "What is wrong with this program" New topic
Author

What is wrong with this program

Abbey Samuel
Greenhorn

Joined: May 02, 2007
Posts: 15
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

Joined: Oct 02, 2003
Posts: 11477
    
  16

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".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dinesh Arora
Greenhorn

Joined: Apr 18, 2007
Posts: 21
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jenson Chew
Ranch Hand

Joined: May 27, 2007
Posts: 55
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 ]

:: Vision is Power ::
Abbey Samuel
Greenhorn

Joined: May 02, 2007
Posts: 15
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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is wrong with this program