 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Frances Del Rio Guest
|
Posted: Tue Sep 21, 2004 3:21 am Post subject: stand-alone classes: have to be always public?? |
|
|
I have been reading all over the place that a stand-alone class always
has to have the name of the file that contains the class, AND that the
class always has to be public.. however, I just realized now, looking at
some stuff I have done as I've been following books, tutorials, etc,
that some of my classes are not public, yet they compile fine.. for
example...
class SumDouble {
public static void main (String args[]) {
double sum = 0;
for (int i = 0; i < args.length; i++) {
String temp = args[i];
double daNumber = Double.valueOf(temp).doubleValue();
sum = sum + daNumber;
}
System.out.println("All the numbers you typed add up to: " + sum);
}
}
this compiles fine, even though I didn't use word "public" anywhere in
this class.. and have other examples, like for example class called
"Jabberwock" from SAMS "Teach Yrslf Java in 21 Days", by Rogers
Cadenhead and Laura Lemay, p.45..
(code: www.francesdelrio.com/java/jabber.html)
(I changed "Jabberwock" to "jabber" -- otherwise code is exactly as it
appears in the book..) so: how come these non-public classes compile
fine even though they're not public?? as I said, this contradicts stuff
I've read.. main method is public, but I thought in addition to that
class also had to be public... pls enlighten me.. thank you... Frances
|
|
| Back to top |
|
 |
Paul Lutus Guest
|
Posted: Tue Sep 21, 2004 4:59 am Post subject: Re: stand-alone classes: have to be always public?? |
|
|
Frances Del Rio wrote:
| Quote: | I have been reading all over the place that a stand-alone class
|
Please explain what you think a "stand-alone class" is.
| Quote: | always
has to have the name of the file that contains the class, AND that the
class always has to be public..
|
Each source file must have a class with the file's name in it, and that
class must be public. There can be other classes ion the same file without
the public access modifier. Also, you have package access that can control
access to those public classes.
| Quote: | however, I just realized now, looking at
some stuff I have done as I've been following books, tutorials, etc,
that some of my classes are not public, yet they compile fine..
|
Define "fine". Are they in a source file with the same class name?
| Quote: | for
example...
class SumDouble {
public static void main (String args[]) {
double sum = 0;
for (int i = 0; i < args.length; i++) {
String temp = args[i];
double daNumber = Double.valueOf(temp).doubleValue();
sum = sum + daNumber;
}
System.out.println("All the numbers you typed add up to: " + sum);
}
}
this compiles fine, even though I didn't use word "public" anywhere in
this class..
|
What is the name of the source file?
| Quote: | and have other examples, like for example class called
"Jabberwock" from SAMS "Teach Yrslf Java in 21 Days", by Rogers
Cadenhead and Laura Lemay, p.45..
(code: www.francesdelrio.com/java/jabber.html)
(I changed "Jabberwock" to "jabber" -- otherwise code is exactly as it
appears in the book..) so: how come these non-public classes compile
fine even though they're not public??
|
Please tell us the name of the source file. Post the source file name for
each of your examples.
| Quote: | as I said, this contradicts stuff
I've read..
|
No, not really.
--
Paul Lutus
http://www.arachnoid.com
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Tue Sep 21, 2004 6:36 am Post subject: Re: stand-alone classes: have to be always public?? |
|
|
On Mon, 20 Sep 2004 21:59:45 -0700, Paul Lutus wrote:
| Quote: | Each source file must have a class with the file's name in it, and that
class must be public.
|
Not so, the following example demonstrates.
MainClass.java
<sscce>
class MainClass {
public static void main(String[] args) {
MinorClass mc1 = new MinorClass();
System.out.println( mc1 );
}
}
class MinorClass {
public String toString() {
return "Minor Class";
}
}
</sscce>
This example, with no package and no public class,
compiles cleanly, and when run, produces the string
"Minor Class".
To Frances. Putting each Java class in it's own file
makes a great deal of sense in just about every situation
except an SSCCE.
I think the reason you are generally being told otherwise
is that, mainly that by the time a developer realizes it
is not mandatory, they are in a far better position to
judge when it is, and is not a good idea.
(Similar to null Layouts)
HTH
--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
|
|
| Back to top |
|
 |
Frances Del Rio Guest
|
Posted: Tue Sep 21, 2004 1:14 pm Post subject: Re: stand-alone classes: have to be always public?? |
|
|
Paul Lutus wrote:
| Quote: | Frances Del Rio wrote:
I have been reading all over the place that a stand-alone class
Please explain what you think a "stand-alone class" is.
|
what I mean is a class that runs on its own, a stand-alone app.. in
example I mentioned below that class is all there is in that file, file
is the name of the class, I compile it in the DOS shell and run it from
there...
| Quote: |
always
has to have the name of the file that contains the class, AND that the
class always has to be public..
Each source file must have a class with the file's name in it, and that
class must be public. There can be other classes ion the same file without
the public access modifier. Also, you have package access that can control
access to those public classes.
however, I just realized now, looking at
some stuff I have done as I've been following books, tutorials, etc,
that some of my classes are not public, yet they compile fine..
Define "fine". Are they in a source file with the same class name?
|
I mean it compiles and runs w/no problems....
| Quote: |
for
example...
class SumDouble {
public static void main (String args[]) {
double sum = 0;
for (int i = 0; i < args.length; i++) {
String temp = args[i];
double daNumber = Double.valueOf(temp).doubleValue();
sum = sum + daNumber;
}
System.out.println("All the numbers you typed add up to: " + sum);
}
}
this compiles fine, even though I didn't use word "public" anywhere in
this class..
What is the name of the source file?
source file is called same as the class.. in ex. above the class is in a |
file called "SumDouble.java" to compile it I type "javac
SumDouble.java".. it runs ok..
| Quote: |
and have other examples, like for example class called
"Jabberwock" from SAMS "Teach Yrslf Java in 21 Days", by Rogers
Cadenhead and Laura Lemay, p.45..
(code: www.francesdelrio.com/java/jabber.html)
(I changed "Jabberwock" to "jabber" -- otherwise code is exactly as it
appears in the book..) so: how come these non-public classes compile
fine even though they're not public??
Please tell us the name of the source file. Post the source file name for
each of your examples.
|
for 'jabber' example file is called 'jabber.java'
(again, code is here, www.francesdelrio.com/java/jabber.html)
thank you for your help... Frances
| Quote: |
as I said, this contradicts stuff
I've read..
No, not really.
|
|
|
| Back to top |
|
 |
George W. Cherry Guest
|
Posted: Tue Sep 21, 2004 11:55 pm Post subject: Re: stand-alone classes: have to be always public?? |
|
|
"Andrew Thompson" <SeeMySites (AT) www (DOT) invalid> wrote
| Quote: | On Mon, 20 Sep 2004 21:59:45 -0700, Paul Lutus wrote:
Each source file must have a class with the file's name in it, and that
class must be public.
Not so, the following example demonstrates.
MainClass.java
sscce
class MainClass {
public static void main(String[] args) {
MinorClass mc1 = new MinorClass();
System.out.println( mc1 );
}
}
class MinorClass {
public String toString() {
return "Minor Class";
}
}
/sscce
|
Well, the following won't compile. (Of course, it's
nonsensical).
private class MainClass {
public static void main(String[] args) {
MinorClass mc1 = new MinorClass();
System.out.println( mc1 );
}
}
class MinorClass {
public String toString() {
return "Minor Class";
}
}
|
|
| Back to top |
|
 |
Frances Del Rio Guest
|
Posted: Wed Sep 22, 2004 3:55 am Post subject: Re: stand-alone classes: have to be always public?? |
|
|
Andrew Thompson wrote:
| Quote: | On Mon, 20 Sep 2004 21:59:45 -0700, Paul Lutus wrote:
Each source file must have a class with the file's name in it, and that
class must be public.
Not so, the following example demonstrates.
MainClass.java
sscce
class MainClass {
public static void main(String[] args) {
MinorClass mc1 = new MinorClass();
System.out.println( mc1 );
}
}
class MinorClass {
public String toString() {
return "Minor Class";
}
}
/sscce
This example, with no package and no public class,
compiles cleanly, and when run, produces the string
"Minor Class".
To Frances. Putting each Java class in it's own file
makes a great deal of sense in just about every situation
except an SSCCE.
I think the reason you are generally being told otherwise
is that, mainly that by the time a developer realizes it
is not mandatory, they are in a far better position to
judge when it is, and is not a good idea.
(Similar to null Layouts)
|
thank you Andrew.. I wish I knew where I read this (possibly on
java.sun... I have been referring a lot to
http://java.sun.com/docs/books/tutorial/reallybigindex.html
which, it seems, contains just about everything.. thanks again..
Frances
|
|
| Back to top |
|
 |
Tony Morris Guest
|
Posted: Wed Sep 22, 2004 6:45 am Post subject: Re: stand-alone classes: have to be always public?? |
|
|
"Frances Del Rio" <fdr58 (AT) yahoo (DOT) com> wrote
| Quote: | I have been reading all over the place that a stand-alone class always
has to have the name of the file that contains the class, AND that the
class always has to be public.. however, I just realized now, looking at
some stuff I have done as I've been following books, tutorials, etc,
that some of my classes are not public, yet they compile fine.. for
example...
class SumDouble {
public static void main (String args[]) {
double sum = 0;
for (int i = 0; i < args.length; i++) {
String temp = args[i];
double daNumber = Double.valueOf(temp).doubleValue();
sum = sum + daNumber;
}
System.out.println("All the numbers you typed add up to: " + sum);
}
}
this compiles fine, even though I didn't use word "public" anywhere in
this class.. and have other examples, like for example class called
"Jabberwock" from SAMS "Teach Yrslf Java in 21 Days", by Rogers
Cadenhead and Laura Lemay, p.45..
(code: www.francesdelrio.com/java/jabber.html)
(I changed "Jabberwock" to "jabber" -- otherwise code is exactly as it
appears in the book..) so: how come these non-public classes compile
fine even though they're not public?? as I said, this contradicts stuff
I've read.. main method is public, but I thought in addition to that
class also had to be public... pls enlighten me.. thank you... Frances
|
Here are some basic rules:
- There are 4 access modifiers; public, package (no modifier specified),
protected, private.
- A top level class may have a public or package access scope.
- A public top level class must be in a source file that corresponds to its
name (e.g. public class X belongs in X.java)
- A nested or inner class can have any of the four access scopes.
--
Tony Morris
http://xdweb.net/~dibblego/
|
|
| Back to top |
|
 |
Stefan Schulz Guest
|
Posted: Wed Sep 22, 2004 10:24 am Post subject: Re: stand-alone classes: have to be always public?? |
|
|
On Tue, 21 Sep 2004 23:55:46 GMT, George W. Cherry
<GWCherryHatesGreenEggsAndSpam (AT) alum (DOT) mit.edu> wrote:
| Quote: | Well, the following won't compile. (Of course, it's
nonsensical).
private class MainClass {
public static void main(String[] args) {
MinorClass mc1 = new MinorClass();
System.out.println( mc1 );
}
}
class MinorClass {
public String toString() {
return "Minor Class";
}
}
|
That is right, because private top-level classes can not be accessed from
anywhere. The compiler
is just telling you that your definition can never be used. However, the
following is perfectly
fine:
class MainClass {
private class MinorClass {
public String toString(){
return "Minor Class";
}
}
public static void main(String [] argv){
MinorClass minor = new MinorClass();
System.out.println(minor);
}
}
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Wed Sep 22, 2004 10:36 am Post subject: Re: stand-alone classes: have to be always public?? |
|
|
On Wed, 22 Sep 2004 12:24:32 +0200, Stefan Schulz wrote:
...
| Quote: | ..the following is perfectly fine:
|
I asked my mate the compiler, and he reckons your wrong..
...
| Quote: | private class MinorClass {
|
private static class MinorClass {
--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
|
|
| Back to top |
|
 |
Stefan Schulz Guest
|
Posted: Wed Sep 22, 2004 10:54 am Post subject: Re: stand-alone classes: have to be always public?? |
|
|
On Wed, 22 Sep 2004 10:36:17 GMT, Andrew Thompson <SeeMySites (AT) www (DOT) invalid>
wrote:
You got me ;)
Right, that is what i get for manually typing instead of copy&paste. (I'm
going to claim the
exact opposite next time i make a copy&paste error )
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Wed Sep 22, 2004 11:03 am Post subject: Re: stand-alone classes: have to be always public?? |
|
|
On Wed, 22 Sep 2004 12:54:32 +0200, Stefan Schulz wrote:
| Quote: | (I'm going to claim the
exact opposite next time i make a copy&paste error )
|
Nahhh. If the copy/paste fails, blame the clipboard. ;-)
|
|
| Back to top |
|
 |
George W. Cherry Guest
|
Posted: Wed Sep 22, 2004 5:09 pm Post subject: Re: stand-alone classes: have to be always public?? |
|
|
"Stefan Schulz" <terra (AT) spacetime (DOT) de> wrote
| Quote: | On Tue, 21 Sep 2004 23:55:46 GMT, George W. Cherry
[email]GWCherryHatesGreenEggsAndSpam (AT) alum (DOT) mit.edu[/email]> wrote:
Well, the following won't compile. (Of course, it's
nonsensical).
private class MainClass {
public static void main(String[] args) {
MinorClass mc1 = new MinorClass();
System.out.println( mc1 );
}
}
class MinorClass {
public String toString() {
return "Minor Class";
}
}
That is right, because private top-level classes can not be accessed from
anywhere. The compiler
is just telling you that your definition can never be used. However, the
following is perfectly
fine:
class MainClass {
private class MinorClass {
public String toString(){
return "Minor Class";
}
}
public static void main(String [] argv){
MinorClass minor = new MinorClass();
System.out.println(minor);
}
}
|
Nope, Stefan. But the following will compile and run.
class MainClass {
private static class MinorClass {
public String toString(){
return "Minor Class";
}
}
public static void main(String [] args){
MinorClass minor = new MinorClass();
System.out.println(minor);
}
}
BTW, Stefan, your (non)indention style makes
your posts hard to read.
George
|
|
| Back to top |
|
 |
George W. Cherry Guest
|
Posted: Wed Sep 22, 2004 5:11 pm Post subject: Re: stand-alone classes: have to be always public?? |
|
|
"Andrew Thompson" <SeeMySites (AT) www (DOT) invalid> wrote
| Quote: | On Wed, 22 Sep 2004 12:54:32 +0200, Stefan Schulz wrote:
(I'm going to claim the
exact opposite next time i make a copy&paste error )
Nahhh. If the copy/paste fails, blame the clipboard.
|
Damn that clipboard! : o )
|
|
| Back to top |
|
 |
Stefan Schulz Guest
|
Posted: Wed Sep 22, 2004 5:18 pm Post subject: Re: stand-alone classes: have to be always public?? |
|
|
On Wed, 22 Sep 2004 17:09:39 GMT, George W. Cherry
<GWCherryHatesGreenEggsAndSpam (AT) alum (DOT) mit.edu> wrote:
| Quote: | Nope, Stefan. But the following will compile and run.
|
[...]
*points the the other branch of the Thread and hands out a silver medal* ;)
| Quote: |
BTW, Stefan, your (non)indention style makes
your posts hard to read.
|
Yeah, you are right. As for the non-indention... it is actually indented
by a tab
a level.
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
|
|
| Back to top |
|
 |
George W. Cherry Guest
|
Posted: Thu Sep 23, 2004 1:22 am Post subject: Re: stand-alone classes: have to be always public?? |
|
|
"Stefan Schulz" <terra (AT) spacetime (DOT) de> wrote
| Quote: | On Wed, 22 Sep 2004 17:09:39 GMT, George W. Cherry
[email]GWCherryHatesGreenEggsAndSpam (AT) alum (DOT) mit.edu[/email]> wrote:
Nope, Stefan. But the following will compile and run.
[...]
*points the the other branch of the Thread and hands out a silver medal*
;)
BTW, Stefan, your (non)indention style makes
your posts hard to read.
Yeah, you are right. As for the non-indention... it is actually indented
by a tab
a level.
|
Damn that fickle tab indention! : o )
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|