Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static class

 
Puneet N Vyas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@
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
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell says, you cannot make a top-level class static.
What are you trying to achieve with the 'static' keyword here?
 
Puneet N Vyas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@
can you explain scenario where static classes are useful,can you give example also..
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap.Entry

At least I think that is what it is called.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
vibhas karn
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pawel Kierat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic