[java] modifiers for class(es).

Started by
10 comments, last by GameDev.net 19 years, 7 months ago
Sorry this is sort of a boring question, but I'm trying to determine if the static keyword is a legal class modifier. And I remember trying in the past to use it on a class and it failing, but I think I misunderstood why it failed. Ever since I believed it could not be used for a class, which would sort of undermine the whole OO thing. However every semantic and BNF page I've been looking at says that its a legal modifier for a class declaration. Although it appears that in all of the ones I have found the modifiers link is generic and ALWAYS jumps to the same modifiers, no matter what the context. Which is poor form, considering a private interface makes no sence. I've also seen static modify a private nested class, in the case of a node. But now I think it might be legal only because its a private nested class. Anybody know the particulars? Thanks, L-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Advertisement
Why do you want a static class? Or is this just out of interest?

Have you read the JLS v2? IIRC it is non-ambiguous.
Static cannot be used for classes or interfaces. It can be used on inner classes. If you are trying to make a class that people cannot create an instance of, just make the constructor private. Is there a reason you want to use it?
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
static class <classname>
{
...
}

//cannot be ( i think so )

//but u can

class <another class>
{
static <classname same as above> <identifier>;
...
}
BNFs are not powerful enough to describe the language's grammar completely; there is some context to worry about.

'static' as a class modifier is intended to be used only on inner classes, where it has the meaning - IIRC - of "don't generate the pseudo-closure bindings to members in the parent class".

Note that you can also just make a non-public class with no modifiers, if you're looking for ways to prevent or control instantiation. Then that class can only be instantiated by other classes within the same package.

One possible organizational pattern, that works well if you already want a Factory, and don't want calling code to have to know about subclasses:

1) Make the base class public, and give it static factory methods.
2) Make all subclasses non-public, in the same file.
3) Create a new package just for this class hierarchy.
Quote:Original post by CaptainJester
Is there a reason you want to use it?


I wasn't thinking of using it. I'm in the middle of writing a code reader, of sorts, and I couldn't find anything that said specifically that public classes couldn't be static. In case my reader might have to parse a public static class. I haven't completed inner classes/interfaces yet. So, I needed some information on some of the more obscure language usages... this unfortunately is one of those usages I was unsure about.

This clears things up though, it seems to be the concesus that only inner classes can be 'static'. Here and on the forum.java.sun.com forum.

Um, how about the keyword 'transient' -- I have never used it... how is it supposed to be used?

Are there any other method keywords like 'throws'? That I might be over looking ( I've checked the JLS and throws seems to be it. ) But something keeps tickling the back of my mind.

Also, I've seen maybe one other place use static{ } block of code. Can someone shed some light on this. I have a feeling it has to do with 'static' things being loaded into memory first, and therefore a static{ } block would get ran -- but it seems well, dangerous in the sense that more than one static{ } block may exist... I really don't know for sure.

Oh, just to clear things up for me -- can 'synchronized' be used as a 'class' modifier. I'm thinking it can't.

Sorry for the flurry of questions... I'll try never to ask them again...


Any help is appreciated,
L-

"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
'synchronized' is mostly used in multithreaded apps. if used in methods, the object calling the method is given the lock, or rather the method is locked. no other object can use it unless the object currently using it drops the lock, or is finishing using the method ( end of line of the method, or encountering the 'return' statement ).

another use of synchronize is during calling the wait() method of an object.

e.g.

synchronized( this )
{
if( a boolean telling it not to run is true )
try
{
wait();
}
catch( InterruptedException ie )
{
... //complain stuff here
}
}

i don't know the explaination for this, but the book says it's for program stability reasons. this is only the uses of 'synchronized' that i know of. hope it helped and hope i didn't get it wrong.
and oh, i didn't see it used as a class modifier. only in some other complex multithreaded apps like the one above, only more complex than that.

about 'transient' ( c++ i think ), java doesn't have it. ( never get to see it used )
about 'throws', it's not what you think it is. it's for exception handling. it can be used for methods if within the method, a possible error could occur. an 'Exception' is an object thrown if an unresolved error occur.

i'll give an example. say if a number is divided by zero, the program will issue an error. usually if you divide a number with zero in java, there's no error depending on how you would use the result of this dividing. like if you only want to see the result, it would print something like '+INFINITY'...

you can create your own Exception...

class DividingZeroException extends Exception //or any other, like RuntimeException, anyway RuntimeException extends Exception
{
public DividingZeroException()
{
super();
}
}

then for a method such as this...

public int divide( int dividend, int divisor )
{
if( divisor == 0 )
throw new DividingZeroException(); //this ends execution of the method. it would be up to the calling body to catch it

return dividend / divisor;
}

simple, isn't it? you could do this

public int divide( int dividend, int divisor ) throws DividingZeroException
{
//the same thing as above
}

it's just the same thing, but writing it this way, one would know that this method throws such Exception and would know how to catch it if he/she would ever use such method.
Quote:Original post by Lucidquiet
Um, how about the keyword 'transient' -- I have never used it... how is it supposed to be used?

transient is used tell the compiler that the variable it applies to should not be serialized. ie save to disk, written to the network, etc...
Quote:Original post by Lucidquiet
Are there any other method keywords like 'throws'? That I might be over looking ( I've checked the JLS and throws seems to be it. ) But something keeps tickling the back of my mind.

private, protected, public, abstract, static, final, synchronized, native, strictfp are the modifiers for methods. Throws comes after the method and that's it.
Quote:Original post by Lucidquiet
Also, I've seen maybe one other place use static{ } block of code. Can someone shed some light on this. I have a feeling it has to do with 'static' things being loaded into memory first, and therefore a static{ } block would get ran -- but it seems well, dangerous in the sense that more than one static{ } block may exist... I really don't know for sure.

The static {...} block can appear anywhere inside the class definition, outside of method definitions. All static blocks are guarunteed top be executed in the order they appear and before any instances of the class exist.
Quote:Original post by Lucidquiet
Oh, just to clear things up for me -- can 'synchronized' be used as a 'class' modifier. I'm thinking it can't.

No.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement