1

I am trying to create static class in Java Web Application using eclipse. but it says

Illegal modifier for the class ClassName; only public, abstract & final are permitted

can we create static class in web application ? if no why ?

Abhijit
  • 673
  • 2
  • 17
  • 35
  • What do you expect to achieve with this "static class"? That all methods in the class are static? Because then you should declare all methods to be static, not the class. – Gimby Sep 06 '13 at 12:10

5 Answers5

5

No, because there are no static top-level classes in Java.

Marcin Łoś
  • 3,226
  • 1
  • 19
  • 21
3

Static class you can create in general only as nested class in another class. This has nothing to do with Web-Application. It has a general validity in Java.

Each compilation unit should contain a public or default (non-static) class. Then within it you can declare static nested classes.

arjacsoh
  • 8,932
  • 28
  • 106
  • 166
  • 1
    `inner` and `static` are mutually exclusive. See [JLS #8.1.3](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3). – user207421 Oct 08 '13 at 01:05
  • @EJP: Ok, I wrote the word with its general meaning, irresprective of how one calls it in Java. I have corrected it. – arjacsoh Oct 08 '13 at 06:35
3

Only inner classes can be made as static. That means except inner class you can not create static class

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • Only *nested* classes can be made static. If you make an inner class static it ceases to be inner. See [JLS #8.1.3](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3). – user207421 Oct 08 '13 at 01:04
  • @EJP I got total 5 upvotes for this answer then this means that those users who gave upvotes accepts that my answer is right.So you should read answer thoroughly once again – SpringLearner Oct 08 '13 at 03:53
  • You should read the part of the Java Language Specification I cited, thoroughly, before commenting again. It doesn't agree with you. – user207421 Oct 08 '13 at 09:21
2

A static class is, in practice, nothing else than a standard non-inner class declared in another class.

In other words, your app web application has nothing to do with classes being declared as static or not.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
1

Outer class cannot be static. Only nested class can be static. For example below is possible

class OuterClass {      
    static class StaticNestedClass {
       ...
    }    
}

But not

static class OuterClass {      
 ...
}
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
  • Only *nested* classes can be made static. If you make an inner class static it ceases to be inner. See [JLS #8.1.3](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3). – user207421 Oct 08 '13 at 01:04
  • @EJP Thanks for your comments and the JLS reference. I had checked [here too](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class). Updated by reply. – Jayamohan Oct 08 '13 at 02:52