-3

This question already has an answer here:

In Java (and in Android), what's the use of static class and final class declarations?

My question is not about static instances but class declarations like,

static class StaticClass {
    //variables and methods
}

and

final class FinalClass {
    //variables and methods
}

Thanks,


6 답변


8

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Final Classes

A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.

http://docs.oracle.com/javase/tutorial/java/IandI/final.html


3

final classes will restrict for further extends (Inherit).

You can not use static keyword on outer class,static is permitted only to inner classes Static classes


1

You make a class final so it can not be extended. Usually it makes sense if you'are creating a library (or working on a part of a big project), so your clients are not able to extend the class and modify the existing behavior. In your own program there's little reason to make a class final unless it's a big program and you can inadvertently forget things.

Static inner classes are for things that logically belongs to an outer (containing) class but which have no dependencies on the state of the outer class. For example you can have a Parser class and an inner Parser.Listener class. Normally if you decide to have an inner class try, first, to make it static, if possible, to simplify things.

You could do without both final and static inner classes then with experience you will find use for them.


  • 'static inner class' is a contradiction in terms. You mean 'static nested'. - user207421

1

other class can not extends the final class exmple String is final class so you cannot extends this class. You cannot have a 'top-level' class declared static. You can only have an inner class with the modifier 'static'. static inner class only access the static member of the outer class


1

if you make a class as final then it can not be inherited. Generally top level class can not be made static however inner class can be made as static and Nested static class doesn’t need reference of Outer class static and final class in java


  • An inner class ceases to be inner if you make it static. You mean 'nested class can be made static'. See JLS #8.1.3. - user207421

0

you can't mark a class static ? yes, if it is nested class no, if it is normal class. if you mark a nested class static it will work as fully flagged class just use a classname.staticClassNmae to access it.

if you mark a class final , it can't be inherited , a good example of this String class in java.

Linked


Related

Latest