Sign in to follow this  
Lucidquiet

[java] modifiers for class(es).

Recommended Posts

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-

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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-

Share this post


Link to post
Share on other sites
'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.

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote]Original post by CaptainJester
thanks again... that helps...

As for the 'throws' thing I just couldn't think of any nomenclature to describe what that would be other than modifier... but I think its the only keyword of its kind.

Thanks again,
L-

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Purpose of static:

When an object is instantiated, its constructor(s) are run.

When a class is loaded, its static block is run.

A class is loaded when it is first referenced in code; if it's never referenced, it (shouldn't - you could write a custom loader and do it anyway) isn't loaded. Generally speaking, once loaded, a class is never unloaded (unless you get rid of the classloader that loaded it). Although that's a gross simplification...

Anyway, the typical practical use of static is that you have static methods that only work if something has been done before they are executed. Classic example is if they use a data structure - e.g. a static ArrayList variable in the class - which will be null by default. With a non-static method, you just make sure you instantiate the variable in the constructor (or in the line where you declare the variable, but there are downsides to doing that such that you often don't want to...); with a static method, you either need to use the same shortcut with the declaration, or else add a static block to the class which contains

... = new ArrayList();

There are also several cunning / arcane uses for that static block to do with funky stuff where you have a real noticeable difference in your application between class loading and object instantiation. It's one of those advanced topics that most people never need and never bother using; I have quite often encountered them when doing custom classloader stuff and complex bootstrapping of JVM's, but rarely outside of that.

redmilamber

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this