package pl.codegym.task.task06.task0617;
/*
Notes na nowe idee
*/
public class Solution { //WHY THIS CLASS IS NON-STATIC?
public static void main(String[] args) {
}
public static void printIdea(Idea anyIdea)
{
}
public static class Idea//WHY THIS CLASS MUST BE STATIC?
{
public String getOpis()
{
}
}
}
Radosław Dziura
Poziom 16
I've done it but I have a few questions
Dyskutowane
Komentarze (1)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Anthony Chalk
29 stycznia 2020, 23:36
the keyword "static" means that something belongs to the class, as opposed to an instance of the class.
With methods and variables this makes sense, however, a class can't belong to itself.
The exception is a nested class (line 19), where you declare a class within a class.
From Oracle's website:
Why Use Nested Classes?
Compelling reasons for using nested classes include the following:
It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
+1