is there any advantage of adding a inner class over a java class in a new file? [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
Difference between static nested class and regular class
3 answers
I would like to know if there any advantage of adding an inner class over a java class in a new file. Assuming these are only the two ways i can achieve that is needed. But I would only like to which one is a better approach of these two.
class ABC{
LMN obj;
public static class xyz{
@customAnnotation LMN lmn;
public void set(ABC abc){
abc.obj = lmn;
}
}
}
or have a separate class like this
public class xyz{
@customAnnotation LMN lmn;
public void set(ABC abc){
abc.obj = lmn;
}
}
java
marked as duplicate by Progman, Arnaud, leonardkraemer, Raedwald
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 17 at 21:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Difference between static nested class and regular class
3 answers
I would like to know if there any advantage of adding an inner class over a java class in a new file. Assuming these are only the two ways i can achieve that is needed. But I would only like to which one is a better approach of these two.
class ABC{
LMN obj;
public static class xyz{
@customAnnotation LMN lmn;
public void set(ABC abc){
abc.obj = lmn;
}
}
}
or have a separate class like this
public class xyz{
@customAnnotation LMN lmn;
public void set(ABC abc){
abc.obj = lmn;
}
}
java
marked as duplicate by Progman, Arnaud, leonardkraemer, Raedwald
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 17 at 21:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
"Assuming these are only the two ways i can achieve that is needed." Why don't you ask the question that actually is your problem then? Maybe the best solution is totally different. Classic xy problem
– leonardkraemer
Jan 3 at 15:29
@leonardkraemer I believe the OP was asking for general knowledge rather than to solve a specific coding issue, but I could be wrong.
– yuvgin
Jan 3 at 15:35
"the two ways" you can also declare multiple top-level classes in a file, provided at most one is public. However, this is discouraged (merely mentioning it to show there are more ways).
– Andy Turner
Jan 3 at 15:59
add a comment |
This question already has an answer here:
Difference between static nested class and regular class
3 answers
I would like to know if there any advantage of adding an inner class over a java class in a new file. Assuming these are only the two ways i can achieve that is needed. But I would only like to which one is a better approach of these two.
class ABC{
LMN obj;
public static class xyz{
@customAnnotation LMN lmn;
public void set(ABC abc){
abc.obj = lmn;
}
}
}
or have a separate class like this
public class xyz{
@customAnnotation LMN lmn;
public void set(ABC abc){
abc.obj = lmn;
}
}
java
This question already has an answer here:
Difference between static nested class and regular class
3 answers
I would like to know if there any advantage of adding an inner class over a java class in a new file. Assuming these are only the two ways i can achieve that is needed. But I would only like to which one is a better approach of these two.
class ABC{
LMN obj;
public static class xyz{
@customAnnotation LMN lmn;
public void set(ABC abc){
abc.obj = lmn;
}
}
}
or have a separate class like this
public class xyz{
@customAnnotation LMN lmn;
public void set(ABC abc){
abc.obj = lmn;
}
}
This question already has an answer here:
Difference between static nested class and regular class
3 answers
java
java
asked Jan 3 at 15:17
NishantNishant
213
213
marked as duplicate by Progman, Arnaud, leonardkraemer, Raedwald
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 17 at 21:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Progman, Arnaud, leonardkraemer, Raedwald
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 17 at 21:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
"Assuming these are only the two ways i can achieve that is needed." Why don't you ask the question that actually is your problem then? Maybe the best solution is totally different. Classic xy problem
– leonardkraemer
Jan 3 at 15:29
@leonardkraemer I believe the OP was asking for general knowledge rather than to solve a specific coding issue, but I could be wrong.
– yuvgin
Jan 3 at 15:35
"the two ways" you can also declare multiple top-level classes in a file, provided at most one is public. However, this is discouraged (merely mentioning it to show there are more ways).
– Andy Turner
Jan 3 at 15:59
add a comment |
"Assuming these are only the two ways i can achieve that is needed." Why don't you ask the question that actually is your problem then? Maybe the best solution is totally different. Classic xy problem
– leonardkraemer
Jan 3 at 15:29
@leonardkraemer I believe the OP was asking for general knowledge rather than to solve a specific coding issue, but I could be wrong.
– yuvgin
Jan 3 at 15:35
"the two ways" you can also declare multiple top-level classes in a file, provided at most one is public. However, this is discouraged (merely mentioning it to show there are more ways).
– Andy Turner
Jan 3 at 15:59
"Assuming these are only the two ways i can achieve that is needed." Why don't you ask the question that actually is your problem then? Maybe the best solution is totally different. Classic xy problem
– leonardkraemer
Jan 3 at 15:29
"Assuming these are only the two ways i can achieve that is needed." Why don't you ask the question that actually is your problem then? Maybe the best solution is totally different. Classic xy problem
– leonardkraemer
Jan 3 at 15:29
@leonardkraemer I believe the OP was asking for general knowledge rather than to solve a specific coding issue, but I could be wrong.
– yuvgin
Jan 3 at 15:35
@leonardkraemer I believe the OP was asking for general knowledge rather than to solve a specific coding issue, but I could be wrong.
– yuvgin
Jan 3 at 15:35
"the two ways" you can also declare multiple top-level classes in a file, provided at most one is public. However, this is discouraged (merely mentioning it to show there are more ways).
– Andy Turner
Jan 3 at 15:59
"the two ways" you can also declare multiple top-level classes in a file, provided at most one is public. However, this is discouraged (merely mentioning it to show there are more ways).
– Andy Turner
Jan 3 at 15:59
add a comment |
5 Answers
5
active
oldest
votes
This is mainly a question of design.
From the JAVA SE Docs:
Why Use Nested Classes?
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.
Meaning if you only need to create and use instances of the class xyz
within the context of the class ABC
, then it makes sense for you to define it as an inner class inside ABC
.
Doing so hides the inner class from the outside world, and gives the outer class access to its private data members (generally speaking, read more below).
This makes your code easier to read and easier to understand, but more importantly it increases encapsulation and data hiding - ensuring the class is only visible to and accessible by those who need to use it.
This is a fundamental principle in Object Oriented Programming.
On a more practical note:
There are two different types of nested classes in Java: static and non-static. Each type defines a different access-privileges relationship with its defining outer class.
You can read more about the differences here.
add a comment |
There are a few advantages, most of which are access control-related:
- Unless this was included inadvertently in your question, the first advantage is right in your post (package-private access).
ABC
being package-private,ABC.xyz
cannot be statically referenced outside the package. So a nested class allows for better access control (xyz
can even beprivate
orprotected
).
ABC.xyz
can access privateABC
members (static ones), which removes the need for exposing encapsulated fields to the world if accessing them fromxyz
is required- Regarding readability, a nested class makes coupling obvious (assuming it is right)
add a comment |
The only real difference is that the static inner class would have access to the private static members and methods in the parent, where the separate class would not.
add a comment |
It is usually used for inner object representations or to offer a public builder if you follow the builder pattern.
add a comment |
- Non-static classes inside a container
The example indeed leads to the advantage of having a non-static inner class:
class ABC {
LMN obj;
List<Xyz> xyzs = new ArrayList<>();
public class Xyz {
@customAnnotation LMN lmn;
public void foo(){
ABC.this.obj = lmn;
}
}
... new Xyz(); // Passes the this of ABC.
}
One might make the constructor of Xyz private in order to allow only ABC the correct creation of Xyzs.
Namely that ever Xyz knows the ABC that created it: ABC.this
. Useful for containers.
- For method/stream related value holders
For static inner classes it can be a local use inside the class itself, or a parameter/result/exception class for a method in ABC, bound to the usage of class ABC.
Such a class is useful, as the API will not change when the parameter of a method is just Xyz, but Xyz gets and additional field. Would the method get an additional field, all usages would need to be updated. For a library that could make backward compatibility more awkward (a new method).
For lambdas with a classical forEach, or for loops such a static class might be useful too.
- Already often used: local enums
Finally enum
s defining a domain value range, are often also local to a class.
- Uses in standard java
In standard java there is the case of Character with extensive Unicode support in the form of inner classes Character.SubSet, UnicodeBlock, UnicodeScript
.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is mainly a question of design.
From the JAVA SE Docs:
Why Use Nested Classes?
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.
Meaning if you only need to create and use instances of the class xyz
within the context of the class ABC
, then it makes sense for you to define it as an inner class inside ABC
.
Doing so hides the inner class from the outside world, and gives the outer class access to its private data members (generally speaking, read more below).
This makes your code easier to read and easier to understand, but more importantly it increases encapsulation and data hiding - ensuring the class is only visible to and accessible by those who need to use it.
This is a fundamental principle in Object Oriented Programming.
On a more practical note:
There are two different types of nested classes in Java: static and non-static. Each type defines a different access-privileges relationship with its defining outer class.
You can read more about the differences here.
add a comment |
This is mainly a question of design.
From the JAVA SE Docs:
Why Use Nested Classes?
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.
Meaning if you only need to create and use instances of the class xyz
within the context of the class ABC
, then it makes sense for you to define it as an inner class inside ABC
.
Doing so hides the inner class from the outside world, and gives the outer class access to its private data members (generally speaking, read more below).
This makes your code easier to read and easier to understand, but more importantly it increases encapsulation and data hiding - ensuring the class is only visible to and accessible by those who need to use it.
This is a fundamental principle in Object Oriented Programming.
On a more practical note:
There are two different types of nested classes in Java: static and non-static. Each type defines a different access-privileges relationship with its defining outer class.
You can read more about the differences here.
add a comment |
This is mainly a question of design.
From the JAVA SE Docs:
Why Use Nested Classes?
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.
Meaning if you only need to create and use instances of the class xyz
within the context of the class ABC
, then it makes sense for you to define it as an inner class inside ABC
.
Doing so hides the inner class from the outside world, and gives the outer class access to its private data members (generally speaking, read more below).
This makes your code easier to read and easier to understand, but more importantly it increases encapsulation and data hiding - ensuring the class is only visible to and accessible by those who need to use it.
This is a fundamental principle in Object Oriented Programming.
On a more practical note:
There are two different types of nested classes in Java: static and non-static. Each type defines a different access-privileges relationship with its defining outer class.
You can read more about the differences here.
This is mainly a question of design.
From the JAVA SE Docs:
Why Use Nested Classes?
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.
Meaning if you only need to create and use instances of the class xyz
within the context of the class ABC
, then it makes sense for you to define it as an inner class inside ABC
.
Doing so hides the inner class from the outside world, and gives the outer class access to its private data members (generally speaking, read more below).
This makes your code easier to read and easier to understand, but more importantly it increases encapsulation and data hiding - ensuring the class is only visible to and accessible by those who need to use it.
This is a fundamental principle in Object Oriented Programming.
On a more practical note:
There are two different types of nested classes in Java: static and non-static. Each type defines a different access-privileges relationship with its defining outer class.
You can read more about the differences here.
edited Jan 3 at 15:40
answered Jan 3 at 15:31


yuvginyuvgin
9971623
9971623
add a comment |
add a comment |
There are a few advantages, most of which are access control-related:
- Unless this was included inadvertently in your question, the first advantage is right in your post (package-private access).
ABC
being package-private,ABC.xyz
cannot be statically referenced outside the package. So a nested class allows for better access control (xyz
can even beprivate
orprotected
).
ABC.xyz
can access privateABC
members (static ones), which removes the need for exposing encapsulated fields to the world if accessing them fromxyz
is required- Regarding readability, a nested class makes coupling obvious (assuming it is right)
add a comment |
There are a few advantages, most of which are access control-related:
- Unless this was included inadvertently in your question, the first advantage is right in your post (package-private access).
ABC
being package-private,ABC.xyz
cannot be statically referenced outside the package. So a nested class allows for better access control (xyz
can even beprivate
orprotected
).
ABC.xyz
can access privateABC
members (static ones), which removes the need for exposing encapsulated fields to the world if accessing them fromxyz
is required- Regarding readability, a nested class makes coupling obvious (assuming it is right)
add a comment |
There are a few advantages, most of which are access control-related:
- Unless this was included inadvertently in your question, the first advantage is right in your post (package-private access).
ABC
being package-private,ABC.xyz
cannot be statically referenced outside the package. So a nested class allows for better access control (xyz
can even beprivate
orprotected
).
ABC.xyz
can access privateABC
members (static ones), which removes the need for exposing encapsulated fields to the world if accessing them fromxyz
is required- Regarding readability, a nested class makes coupling obvious (assuming it is right)
There are a few advantages, most of which are access control-related:
- Unless this was included inadvertently in your question, the first advantage is right in your post (package-private access).
ABC
being package-private,ABC.xyz
cannot be statically referenced outside the package. So a nested class allows for better access control (xyz
can even beprivate
orprotected
).
ABC.xyz
can access privateABC
members (static ones), which removes the need for exposing encapsulated fields to the world if accessing them fromxyz
is required- Regarding readability, a nested class makes coupling obvious (assuming it is right)
edited Jan 3 at 15:34
answered Jan 3 at 15:26
ernest_kernest_k
24.7k43151
24.7k43151
add a comment |
add a comment |
The only real difference is that the static inner class would have access to the private static members and methods in the parent, where the separate class would not.
add a comment |
The only real difference is that the static inner class would have access to the private static members and methods in the parent, where the separate class would not.
add a comment |
The only real difference is that the static inner class would have access to the private static members and methods in the parent, where the separate class would not.
The only real difference is that the static inner class would have access to the private static members and methods in the parent, where the separate class would not.
answered Jan 3 at 15:22
robkinrobkin
414
414
add a comment |
add a comment |
It is usually used for inner object representations or to offer a public builder if you follow the builder pattern.
add a comment |
It is usually used for inner object representations or to offer a public builder if you follow the builder pattern.
add a comment |
It is usually used for inner object representations or to offer a public builder if you follow the builder pattern.
It is usually used for inner object representations or to offer a public builder if you follow the builder pattern.
answered Jan 3 at 15:26


Clément BarethClément Bareth
484
484
add a comment |
add a comment |
- Non-static classes inside a container
The example indeed leads to the advantage of having a non-static inner class:
class ABC {
LMN obj;
List<Xyz> xyzs = new ArrayList<>();
public class Xyz {
@customAnnotation LMN lmn;
public void foo(){
ABC.this.obj = lmn;
}
}
... new Xyz(); // Passes the this of ABC.
}
One might make the constructor of Xyz private in order to allow only ABC the correct creation of Xyzs.
Namely that ever Xyz knows the ABC that created it: ABC.this
. Useful for containers.
- For method/stream related value holders
For static inner classes it can be a local use inside the class itself, or a parameter/result/exception class for a method in ABC, bound to the usage of class ABC.
Such a class is useful, as the API will not change when the parameter of a method is just Xyz, but Xyz gets and additional field. Would the method get an additional field, all usages would need to be updated. For a library that could make backward compatibility more awkward (a new method).
For lambdas with a classical forEach, or for loops such a static class might be useful too.
- Already often used: local enums
Finally enum
s defining a domain value range, are often also local to a class.
- Uses in standard java
In standard java there is the case of Character with extensive Unicode support in the form of inner classes Character.SubSet, UnicodeBlock, UnicodeScript
.
add a comment |
- Non-static classes inside a container
The example indeed leads to the advantage of having a non-static inner class:
class ABC {
LMN obj;
List<Xyz> xyzs = new ArrayList<>();
public class Xyz {
@customAnnotation LMN lmn;
public void foo(){
ABC.this.obj = lmn;
}
}
... new Xyz(); // Passes the this of ABC.
}
One might make the constructor of Xyz private in order to allow only ABC the correct creation of Xyzs.
Namely that ever Xyz knows the ABC that created it: ABC.this
. Useful for containers.
- For method/stream related value holders
For static inner classes it can be a local use inside the class itself, or a parameter/result/exception class for a method in ABC, bound to the usage of class ABC.
Such a class is useful, as the API will not change when the parameter of a method is just Xyz, but Xyz gets and additional field. Would the method get an additional field, all usages would need to be updated. For a library that could make backward compatibility more awkward (a new method).
For lambdas with a classical forEach, or for loops such a static class might be useful too.
- Already often used: local enums
Finally enum
s defining a domain value range, are often also local to a class.
- Uses in standard java
In standard java there is the case of Character with extensive Unicode support in the form of inner classes Character.SubSet, UnicodeBlock, UnicodeScript
.
add a comment |
- Non-static classes inside a container
The example indeed leads to the advantage of having a non-static inner class:
class ABC {
LMN obj;
List<Xyz> xyzs = new ArrayList<>();
public class Xyz {
@customAnnotation LMN lmn;
public void foo(){
ABC.this.obj = lmn;
}
}
... new Xyz(); // Passes the this of ABC.
}
One might make the constructor of Xyz private in order to allow only ABC the correct creation of Xyzs.
Namely that ever Xyz knows the ABC that created it: ABC.this
. Useful for containers.
- For method/stream related value holders
For static inner classes it can be a local use inside the class itself, or a parameter/result/exception class for a method in ABC, bound to the usage of class ABC.
Such a class is useful, as the API will not change when the parameter of a method is just Xyz, but Xyz gets and additional field. Would the method get an additional field, all usages would need to be updated. For a library that could make backward compatibility more awkward (a new method).
For lambdas with a classical forEach, or for loops such a static class might be useful too.
- Already often used: local enums
Finally enum
s defining a domain value range, are often also local to a class.
- Uses in standard java
In standard java there is the case of Character with extensive Unicode support in the form of inner classes Character.SubSet, UnicodeBlock, UnicodeScript
.
- Non-static classes inside a container
The example indeed leads to the advantage of having a non-static inner class:
class ABC {
LMN obj;
List<Xyz> xyzs = new ArrayList<>();
public class Xyz {
@customAnnotation LMN lmn;
public void foo(){
ABC.this.obj = lmn;
}
}
... new Xyz(); // Passes the this of ABC.
}
One might make the constructor of Xyz private in order to allow only ABC the correct creation of Xyzs.
Namely that ever Xyz knows the ABC that created it: ABC.this
. Useful for containers.
- For method/stream related value holders
For static inner classes it can be a local use inside the class itself, or a parameter/result/exception class for a method in ABC, bound to the usage of class ABC.
Such a class is useful, as the API will not change when the parameter of a method is just Xyz, but Xyz gets and additional field. Would the method get an additional field, all usages would need to be updated. For a library that could make backward compatibility more awkward (a new method).
For lambdas with a classical forEach, or for loops such a static class might be useful too.
- Already often used: local enums
Finally enum
s defining a domain value range, are often also local to a class.
- Uses in standard java
In standard java there is the case of Character with extensive Unicode support in the form of inner classes Character.SubSet, UnicodeBlock, UnicodeScript
.
answered Jan 3 at 15:49


Joop EggenJoop Eggen
79k755105
79k755105
add a comment |
add a comment |
"Assuming these are only the two ways i can achieve that is needed." Why don't you ask the question that actually is your problem then? Maybe the best solution is totally different. Classic xy problem
– leonardkraemer
Jan 3 at 15:29
@leonardkraemer I believe the OP was asking for general knowledge rather than to solve a specific coding issue, but I could be wrong.
– yuvgin
Jan 3 at 15:35
"the two ways" you can also declare multiple top-level classes in a file, provided at most one is public. However, this is discouraged (merely mentioning it to show there are more ways).
– Andy Turner
Jan 3 at 15:59