In Java, a class declared in another class is called an nested class.
There are two types of nested classes: static and non-static .
A static nested class ( static nested class ) is a class declared with the static modifier.
A non-static nested class (also called an inner class, an inner class ) is, in fact, a class declared without the static modifier.
Why do we create an instance of a static class?
A non-static nested class (inner class) implicitly contains a reference to an instance of an outer class, thereby achieving access to non-static fields of the outer class from the inner class.
Since the inner class contains a reference to an instance of the outer class, an instance of the inner class can only be created in non-static methods (or blocks) of the outer class.
Since you are creating an instance of a nested class in a static method of an outer class, i.e. without creating an instance of the outer class, this nested class should be static .
The following code does not compile:
In this case, you are trying to call the non-static translate() method without creating an instance of the class, which cannot be done.
It would seem that the solution to this problem is to declare the translate() and getLanguage() methods static, but in this case this option will not work, since the getLanguage() method is an abstract method and the abstract method cannot be declared static.
If we remove the static modifier from the EnglishTranslator class, we get the following error:
Solution.this cannot be referenced from a static context
If you remove the static modifier from the EnglishTranslator class, then this class will become an internal class, and the internal class, as mentioned above, implicitly contains a reference to an instance of the external class.
You create an instance of the EnglishTranslator class in a static external class method that can be called without creating an external class instance, therefore, you cannot refer to this external class in this method, which means that you cannot create an instance of the internal EnglishTranslator class in this method.
We can also render Translator and EnglishTranslator for Solution and remove the static modifier from them; this also compiles and works.
In this case, these classes will cease to be nested, and their instances can be created in the static and non-static methods of other classes.
And the most important thing
Learning the Java language ( actually, like any other programming language ) without reading the relevant classic books is a very, very bad idea .
Articles on the Internet (which may be incorrect, for example, some of them mistakenly stated that objects are passed to Java when called by reference), all sorts of video lessons there (this is generally for the lazy, like watching a movie with a bundle of chips and a can of cola ) - this is not something that should be used to study such fundamental things.
Yes, you can read some articles about some points, but there should always be an authoritative guide in priority.
Regarding JavaRush - yes, the guys provide a good practice on the language, but before doing these practical things, you just need to master the theory on the chosen topic in the book.
So I advise you to always keep on hand (not only keep, but also delve :)) something from the classics of Java, for example: Schildt, Horstmann, Ekkel.