*
The moose likes Beginning Java and the fly likes static class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static class " Watch "static class " New topic
Author

static class

Puneet N Vyas
Ranch Hand

Joined: Sep 20, 2007
Posts: 61
why can't we declare
public static class example
{
int x=123;
}
this throws the error,can any one tell why this gives error

thanks for support
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
What error? Please supply fuller details.

Are you trying to declare a top-level class as "static?" "Static means it belongs to the class it is in, not to any instance of that class. If it isn't "in" a class, how can it "belong to" that class and how can it be "static?"
Puneet N Vyas
Ranch Hand

Joined: Sep 20, 2007
Posts: 61
@
i just saved this class with class name xyz and saved that file with xyz.java
the error i got is:
modifier static not allowed here, at the point where i declared the class,i.e on line
public static class xyz, this is the code i wrote




}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

As Campbell says, you cannot make a top-level class static.
What are you trying to achieve with the 'static' keyword here?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Puneet N Vyas
Ranch Hand

Joined: Sep 20, 2007
Posts: 61
@
can you explain scenario where static classes are useful,can you give example also..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
HashMap.Entry

At least I think that is what it is called.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
All classes are implicitly static. [Edit: see Jim's clarification below]
The static keyword is therefore not required or allowed.

You can search through the spec for references to class and to static.
[ April 22, 2008: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Campbell]: HashMap.Entry

At least I think that is what it is called.


Map.Entry

It would have been possible to make Entry a top-level interface. But by making it a nested part of the Map interface, that communicates that Entry is specifically only used in conjunction with a Map. It's a way to group related things together to make them easier to find and understand.

Nested classes (including inner classes) also have the ability to access private fields of the enclosing class. So if you need a class to have access to private data of another class, you might make it a static nested class, or an inner class. That's not a very common technique, but it is possible.

[Bill]: All classes are implicitly static. The static keyword is therefore not required or allowed.

Perhaps you meant to say that all top-level classes are implicitly static, since it's obviously not true for inner classes. It's not really true for top-level classes either, but at least it's fair to say that top-level classes are like static classes (or vice versa).
[ April 22, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
vibhas karn
Greenhorn

Joined: Aug 11, 2008
Posts: 26
Thans alot all of you i got to the conclusion that the top class can't be static because static is by default belongs to class and not the the object (thats of sure). M i right ? But static class is possible if we write like this---->

public class fruit
{
String kind;

public static class apple
{

// anycode;

}
}
Shahnawaz Shakil
Ranch Hand

Joined: Aug 04, 2008
Posts: 57
Yes vibhas,
you are correct in saying that static class is possible if we write like this-->

But one thing which is important to note here is that a static "nested" class is simply a class that is a static member of enclosing class.
"There is nothing in the this world known as static class". The static modifier in this case simply says that the class apple is static member of class fruit.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
Thank you, but I suspect the previous posters have lost interest in a thread from last April. Please look at this FAQ.
[ August 13, 2008: Message edited by: Campbell Ritchie ]
vibhas karn
Greenhorn

Joined: Aug 11, 2008
Posts: 26
Originally posted by Shahnawaz Shakil:
Yes vibhas,
you are correct in saying that static class is possible if we write like this-->

But one thing which is important to note here is that a static "nested" class is simply a class that is a static member of enclosing class.
"There is nothing in the this world known as static class". The static modifier in this case simply says that the class apple is static member of class fruit.
vibhas karn
Greenhorn

Joined: Aug 11, 2008
Posts: 26
Originally posted by Shahnawaz Shakil:
Yes vibhas,
you are correct in saying that static class is possible if we write like this-->

But one thing which is important to note here is that a static "nested" class is simply a class that is a static member of enclosing class.
"There is nothing in the this world known as static class". The static modifier in this case simply says that the class apple is static member of class fruit.




Thanks alot Shanwaz for this information that the above static class is itself a member of the class can you tell me one thing more why you can't declare an interface like this

public static interface Fruit
{
void colour();
......
}
please give me some example if you can...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38497
    
  23
Originally posted by vibhas karn:
Why you can't declare an interface like this

public static interface Fruit
{
void colour();
......
}
please give me some example if you can...
Of course you can . . . but only as a nested interface. Can't think of any examples-sorry.
Pawel Kierat
Greenhorn

Joined: Jan 28, 2012
Posts: 2
Jim Yingst wrote:
[Bill]: All classes are implicitly static. The static keyword is therefore not required or allowed.


So are member interfaces and enums, yet the static keyword is allowed (though not required) there. I think it is a little bit inconsistent in Java.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7711
    
  20

Pawel Kierat wrote:So are member interfaces and enums, yet the static keyword is allowed (though not required) there. I think it is a little bit inconsistent in Java.

No, for member (ie, nested) classes, there's a big difference between static and non-static. Usually, you want the first, but not always.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Pawel Kierat
Greenhorn

Joined: Jan 28, 2012
Posts: 2
Winston Gutkowski wrote:
No, for member (ie, nested) classes, there's a big difference between static and non-static. Usually, you want the first, but not always.

It's true. What I meant is that for nested interfaces the keyword is allowed, even if it does nothing. So why not to allow this keyword for top-level classes too?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7711
    
  20

Pawel Kierat wrote:What I meant is that for nested interfaces the keyword is allowed, even if it does nothing. So why not to allow this keyword for top-level classes too?

If I had to guess, I'd say it was because the writers of the language wanted to use the same syntax for classes and interfaces.

But, at the end of the day, all these 'Why does Java do this?' questions have the same answer: Ask the designers.

Anything you get from a forum like this that isn't backed up by a reference to the JLS is likely to be mere conjecture.

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fundupuneet vyas wrote:why can't we declare
public static class example


You can if it's a nested class, but you can't for a top-level class, because of what "static" means. It means "associated with the enclosing class overall, not with individual instances of that class." For instance, here:


declaring variable "i" to be static means it's associated with the class "Foo" (the enclosing class) as a whole.

If you just do

there is no enclosing class, so the whole concept of being static or not doesn't apply.

this throws the error,can any one tell why this gives error


In this case, it was easy to guess what the problem was, but for future reference, I second Campbell's suggestion that you TellTheDetails.(⇐click) There are lots of different errors that can occur and lots of different reasons for them. The more details you provide, the easier it is for people to help you.
 
 
subject: static class