Thursday, January 27, 2011

Can Yous Brand A Flat Static Inwards Java? Interview Question

This is i of the tricky questions inwards Java because in that place is no straight forward. Yes in addition to No reply here. You cannot brand a top-level aeroplane static inwards Java, but Yes, you tin plow over the sack brand a nested aeroplane 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 query comes what is a top-level class, in addition to what is a nested aeroplane inwards Java? Well, you lot tin plow over the sack declare multiple classes inwards a unmarried Java source file. Influenza A virus subtype H5N1 aeroplane is said to survive the exceed aeroplane if it is non within whatever other class, in addition to a aeroplane that is within roughly other aeroplane is known every bit nested class. You tin plow over the sack do a nested aeroplane within roughly other top-level or roughly other nested aeroplane inwards Java. This is the aeroplane that tin plow over the sack survive static inwards Java.

Some of you lot must survive wondering what the do goodness of making a aeroplane static inwards Java is? Why would i brand a aeroplane static? In lodge to reply this question, let's revise the concept of what does static hateful inwards Java? Well, a static keyword tin plow over the sack survive used amongst a class, method or variable inwards Java in addition to anything static belongs to the class, which agency a static method tin plow over the sack survive accessed in addition to called simply past times using aeroplane name, i.e. you lot don't postulate to do an illustration of the aeroplane to telephone telephone this method because it belongs to class.

The same is truthful amongst static fellow member variable every bit well; you lot tin plow over the sack access them without creating an illustration of the class. They tin plow over the sack survive accessed past times using aeroplane name, e.g. Class.variable.

Influenza A virus subtype H5N1 non-static nested class, every bit good known every bit the Inner class on the mitt ever postulate an illustration of enclosing class, which agency you lot cannot do an illustration of the nested aeroplane without start creating an illustration of the outer class.

This challenge tin plow over the sack survive overcome past times declaring the nested aeroplane static inwards Java i.e. you lot tin plow over the sack straight do an illustration of a nested aeroplane without start creating an object of enclosing class.


This is the unmarried most of import argue fhy you lot should brand na ested aeroplane static inwards Java , nd this is what advised past times Joshua Bloch inwards his aall-timeclassic Java book, Effective Java.

Like whatever other inwardness Java question, nosotros should verify if compiler back upwardly theses fact or not. To banking concern fit whether nosotros can brand a aeroplane static inwards Java or not, we'll effort both exceed aeroplane in addition to a nested aeroplane static.


Using static modifier amongst a exceed aeroplane class

Let's effort to role the static modifier amongst a exceed aeroplane storey i.e. a aeroplane which is non within whatever other class. Here is the code to do that, when you lot compile this code inwards Java from the dominance trouble you lot volition meet compile fourth dimension fault because this is non permitted inwards Java:

static aeroplane Top{  ... }

In Eclipse, every bit presently every bit type the code, I have next compiler error "Illegal modifier for the aeroplane Top; solely public, abstract & concluding are permitted". You volition have something similar when you lot compile this aeroplane using javac from the command line.




Using static modifier amongst a nested aeroplane inwards Java

Now, let's effort to role the static modifier amongst a nested aeroplane inwards Java i.e. a aeroplane which is within roughly other class. Here is the code to do that in addition to this fourth dimension you lot won't meet whatever compile fourth dimension error. I'll every bit good exhibit you lot how to do an illustration of the nested static aeroplane inwards Java. Influenza A virus subtype H5N1 nested aeroplane is named past times joining the refer of the top aeroplane class in addition to nested aeroplane amongst a point every bit shown below:

class Top{  static class Nested{  ...... } }  class Test{  public static void main(String args[]){    Top.Nested n = new Top.Nested(); // ok } }

You tin plow over the sack meet that in that place is no compile fourth dimension fault this time, which agency you lot tin plow over the sack brand a nested aeroplane static inwards Java. In fact, it is every bit good a best exercise to prefer nested static aeroplane over a non-static aeroplane inwards Java because of convenience (see Effective Java).

 inwards Java because in that place is no straight forrard Can you lot Make a Class Static inwards Java? Interview Question

As you lot tin plow over the sack see, you lot don't postulate an illustration of enclosing or exceed aeroplane storey to do an illustration of the nested static class. Many Java classes follow this exercise e.g. LinkedList.Node in addition to Map.Entry is 2 pop examples of nested static aeroplane from JDK.


That's all nearly whether you lot tin plow over the sack brand a aeroplane static inwards Java. Influenza A virus subtype H5N1 static aeroplane is possible inwards Java but solely when if it is nested, you lot tin plow over the sack non brand a exceed aeroplane storey static inwards Java. Btw, don't confuse the give-and-take hither amongst a aeroplane amongst simply static methods e.g. java.lang.Math, they are every bit good known every bit a static class but that is the totally dissimilar thing.

All the give-and-take hither nearly whether you lot tin plow over the sack role the static modifier amongst a aeroplane or non in addition to every bit I explained, you lot tin plow over the sack solely role the static modifier amongst a nested aeroplane inwards Java. Trying to role a static modifier amongst a exceed aeroplane storey inwards Java.

Further Learning
Can you lot overload a static method inwards Java?
Can you lot override a static method inwards Java?
Difference betwixt the static in addition to non-static method inwards Java?
How populace static concluding variable plant inwards Java?
Difference betwixt constructor in addition to static manufacturing flora method?
Difference betwixt static in addition to dynamic binding?

Thanks a lot for reading this article thence far. If you lot similar this article thence delight portion amongst your friends in addition to colleagues, if you lot accept whatever query or feedback thence delight driblet a comment. 

No comments:

Post a Comment