Can Y'all Brand A Class Static Inwards Java? Interview Question
This is i of the tricky questions inwards Java because at that spot is no straight forwards Yes too No respond here. You cannot brand a move past times bird floor static inwards Java but Yes, you tin terminate brand a nested bird static inwards Java. In fact, it is fifty-fifty advised (see Effective Java) to prefer a nested static class inwards Java than normal inner classes. Now, the enquiry comes what is a move past times bird floor too what is a nested bird inwards Java? Well, yous tin terminate declare multiple classes inwards a unmarried Java source file. Influenza A virus subtype H5N1 bird is said to last move past times bird if it is non within whatever other bird too a bird which is within approximately other bird is known equally nested class. You tin terminate do nested bird within approximately other move past times bird or approximately other nested bird inwards Java. This is the bird which tin terminate last static inwards Java.
Some of yous must last wondering what is the do goodness of making a bird static inwards Java? Why would i brand a bird static? In corporation to respond this question, let's revise the concept of what does static hateful inwards Java? Well, a static keyword tin terminate last used amongst a class, method or variable inwards Java too anything static belongs to the class, which agency a static method tin terminate last accessed too called simply past times using bird cite i.e. yous don't postulate to do an illustration of the bird to telephone band this method because it belongs to class.
Same is truthful amongst static fellow member variable equally well, yous tin terminate access them without creating whatever illustration of the class. They tin terminate last accessed past times using bird cite e.g. Class.variable.
Influenza A virus subtype H5N1 non-static nested class, besides known equally the Inner class on the manus ever postulate an illustration of enclosing class, which agency yous cannot do an illustration of the nested bird without starting fourth dimension creating an illustration of the outer class.
This challenge tin terminate last overcome past times declaring the nested bird static inwards Java i.e yous tin terminate straight do an illustration of nested bird without starting fourth dimension creating an object of enclosing class.
This is the unmarried most of import argue for why yous should brand nested bird static inwards Java too this is what advised past times Joshua Bloch inwards his all fourth dimension classic Java book, Effective Java.
Like whatever other gist Java question, nosotros should verify if compiler back upward theses fact or not. To banking concern tally whether nosotros can brand a bird static inwards Java or not, we'll effort both move past times bird too a nested bird static.
In Eclipse, equally presently equally type the code, I have next compiler error "Illegal modifier for the bird Top; solely public, abstract & terminal are permitted". You volition have something similar when yous compile this bird using javac from the command line.
You tin terminate come across that at that spot is no compile fourth dimension fault this time, which agency yous tin terminate brand a nested bird static inwards Java. In fact, it is besides a best exercise to prefer nested static bird over a non-static bird inwards Java because of convenience (see Effective Java).
As yous tin terminate see, yous don't postulate an illustration of enclosing or move past times bird floor to do an illustration of the nested static class. Many Java classes follow this exercise e.g. LinkedList.Node too Map.Entry is 2 pop examples of nested static bird from JDK.
That's all close whether yous tin terminate brand a bird static inwards Java. Influenza A virus subtype H5N1 static bird is possible inwards Java but solely when if it is nested, yous tin terminate non brand a move past times bird floor static inwards Java. Btw, don't confuse the give-and-take hither amongst a bird amongst simply static methods e.g. java.lang.Math, they are besides known equally a static class but that is the totally unlike thing.
All the give-and-take hither close whether yous tin terminate role the static modifier amongst a bird or non too equally I explained, yous tin terminate solely role the static modifier amongst a nested bird inwards Java. Trying to role a static modifier amongst a move past times bird floor inwards Java.
Further Learning
Can yous overload a static method inwards Java?
Can yous override a static method inwards Java?
Difference betwixt the static too non-static method inwards Java?
How world static terminal variable industrial plant inwards Java?
Difference betwixt constructor too static manufacturing flora method?
Difference betwixt static too dynamic binding?
Thanks a lot for reading this article thus far. If yous similar this article too then delight percentage amongst your friends too colleagues, if yous convey whatever enquiry or feedback too then delight driblet a comment.
Some of yous must last wondering what is the do goodness of making a bird static inwards Java? Why would i brand a bird static? In corporation to respond this question, let's revise the concept of what does static hateful inwards Java? Well, a static keyword tin terminate last used amongst a class, method or variable inwards Java too anything static belongs to the class, which agency a static method tin terminate last accessed too called simply past times using bird cite i.e. yous don't postulate to do an illustration of the bird to telephone band this method because it belongs to class.
Same is truthful amongst static fellow member variable equally well, yous tin terminate access them without creating whatever illustration of the class. They tin terminate last accessed past times using bird cite e.g. Class.variable.
Influenza A virus subtype H5N1 non-static nested class, besides known equally the Inner class on the manus ever postulate an illustration of enclosing class, which agency yous cannot do an illustration of the nested bird without starting fourth dimension creating an illustration of the outer class.
This challenge tin terminate last overcome past times declaring the nested bird static inwards Java i.e yous tin terminate straight do an illustration of nested bird without starting fourth dimension creating an object of enclosing class.
This is the unmarried most of import argue for why yous should brand nested bird static inwards Java too this is what advised past times Joshua Bloch inwards his all fourth dimension classic Java book, Effective Java.
Like whatever other gist Java question, nosotros should verify if compiler back upward theses fact or not. To banking concern tally whether nosotros can brand a bird static inwards Java or not, we'll effort both move past times bird too a nested bird static.
Using static modifier amongst a move past times bird class
Let's effort to role the static modifier amongst a move past times bird floor i.e. a bird which is non within whatever other class. Here is the code to do that, when yous compile this code inwards Java from the ascendance trouble yous volition come across compile fourth dimension fault because this is non permitted inwards Java:static bird Top{ ... }
In Eclipse, equally presently equally type the code, I have next compiler error "Illegal modifier for the bird Top; solely public, abstract & terminal are permitted". You volition have something similar when yous compile this bird using javac from the command line.
Using static modifier amongst a nested bird inwards Java
Now, let's effort to role the static modifier amongst a nested bird inwards Java i.e. a bird which is within approximately other class. Here is the code to do that too this fourth dimension yous won't come across whatever compile fourth dimension error. I'll besides demo yous how to do an illustration of the nested static bird inwards Java. Influenza A virus subtype H5N1 nested bird is named past times joining the cite of the top bird class too nested bird amongst a point equally shown below:class Top{ static class Nested{ ...... } } class Test{ public static void main(String args[]){ Top.Nested n = new Top.Nested(); // ok } }
You tin terminate come across that at that spot is no compile fourth dimension fault this time, which agency yous tin terminate brand a nested bird static inwards Java. In fact, it is besides a best exercise to prefer nested static bird over a non-static bird inwards Java because of convenience (see Effective Java).
As yous tin terminate see, yous don't postulate an illustration of enclosing or move past times bird floor to do an illustration of the nested static class. Many Java classes follow this exercise e.g. LinkedList.Node too Map.Entry is 2 pop examples of nested static bird from JDK.
That's all close whether yous tin terminate brand a bird static inwards Java. Influenza A virus subtype H5N1 static bird is possible inwards Java but solely when if it is nested, yous tin terminate non brand a move past times bird floor static inwards Java. Btw, don't confuse the give-and-take hither amongst a bird amongst simply static methods e.g. java.lang.Math, they are besides known equally a static class but that is the totally unlike thing.
All the give-and-take hither close whether yous tin terminate role the static modifier amongst a bird or non too equally I explained, yous tin terminate solely role the static modifier amongst a nested bird inwards Java. Trying to role a static modifier amongst a move past times bird floor inwards Java.
Further Learning
Can yous overload a static method inwards Java?
Can yous override a static method inwards Java?
Difference betwixt the static too non-static method inwards Java?
How world static terminal variable industrial plant inwards Java?
Difference betwixt constructor too static manufacturing flora method?
Difference betwixt static too dynamic binding?
Thanks a lot for reading this article thus far. If yous similar this article too then delight percentage amongst your friends too colleagues, if yous convey whatever enquiry or feedback too then delight driblet a comment.
0 Response to "Can Y'all Brand A Class Static Inwards Java? Interview Question"
Post a Comment