aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes modifiers for top-level class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "modifiers for top-level class?" Watch "modifiers for top-level class?" New topic
Author

modifiers for top-level class?

simeonshi
Greenhorn

Joined: Dec 09, 2001
Posts: 2
en...
there will be at most one "public" class in a source file.
but could "protected","private" exist?
it is not mentioned in SL-275
could someone be kind enough to help me?
3x
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
The best way to answer a question like this would be to write some code and try to compile it.
Mathew Kuruvilla
Ranch Hand

Joined: Nov 27, 2001
Posts: 135
I need to know the answer to this one too. I mean, I can post code like this: b=0; --b-b--+b>>256, :-)
I need to know if normal classes cannot be protected and private while this is not true for inner classes. That is inner classes can be private or protected, but not synchronized or native.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
simeonshi,
please read the Javaranch Naming Policy and register again.
Thank you for your cooperation.
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
simeonshi
Greenhorn

Joined: Dec 09, 2001
Posts: 2
thanx a lot for u
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Oh yeah, to find out how the java language works, RTFM: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Mathew Kuruvilla:
I need to know the answer to this one too.

inner classes can be private or protected, but not synchronized or native.

<pre>
private Class Test
{
//
}
</pre>

Does this compile?

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mathew Kuruvilla
Ranch Hand

Joined: Nov 27, 2001
Posts: 135
OK, OK.
I compiled the following code successfully.

Ergo, inner classes can be private and protected.
I failed to compile the following:

In each case I got the error:
Test.java: <line number> modifier <modifier> not allowed here ....
where modified was, depending on the class that was compiled, either private, protected, synchronized or native.

That's a lot of code that I compiled in recent days. :-)
Vinny Chun
Greenhorn

Joined: Dec 06, 2001
Posts: 24
Please correct me if I am wrong, if I have several classes in one java file, the access modifier of the first class must be declared as public. If I leave the first class as default and declare the second class as public, I have a compiler error.
class defaultClass { }
public class public Class { }
class OuterA { }
class OuterB { }
Why is that?
Simeon Shi
Ranch Hand

Joined: Dec 09, 2001
Posts: 35
will it?
i tried ur code, it compiled fine except the runtime error coz the missing 'main' method.
plez attention ur 'public class publicClass'
~~~~no blank here
Mathew Kuruvilla
Ranch Hand

Joined: Nov 27, 2001
Posts: 135
Well, the only that needs to be consistent is the NAME of the .java file and the PUBLIC CLASS in that file. You can define any number of class without being 'public' in a file having any name. If you never add a public class to that file, it will always compile. However, when you add that 'public' class, make sure that it matches the file name. The compiler looks at the name of the public class and the name of the file to determine if there is an error. I have seen a related question in the mock exam.
 
wood burning stoves
 
subject: modifiers for top-level class?