Can Nosotros Declare A Class Static Inwards Java?

The reply to this query is both Yes and No, depending on whether y'all are talking almost a overstep score class or a nested class inward Java. You cannot brand a overstep score class static inward Java, the compiler volition non allow it, precisely y'all tin post away brand a nested class static inward Java. H5N1 overstep score class is a class which is non within to a greater extent than or less other class. It may or may non hold upwards world i.e. y'all tin post away convey to a greater extent than than ane class inward a Java beginning file as well as alone needs to hold upwards public, whose refer must hold upwards same every bit the refer of the file, residual of the class or interface on that file may or may non hold upwards public. On the other hand, a nested class is a class within a overstep score class. It is every bit good known every bit the inner class or fellow member class inward Java.

Now, let's cry upwards almost what is the pregnant of static class, why would someone desire to brand a class static inward Java? If y'all are familiar alongside concept of static method as well as static variable inward Java, as well as therefore y'all know that anything static tin post away hold upwards accessed without creating event of the class.

For example, y'all tin post away access the static variable conduct every bit ClassName.variable as well as y'all tin post away invoke the static method every bit ClassName.staticMethod(), this is a slap-up convenience for calling utility method or accessing constants.

This convenience is the primary argue Java programmers similar to declare nested class every bit static inward Java. Remember, H5N1 nested class, if it's non static as well as therefore y'all can't create its event without get-go creating an event of the outer class, which is a fleck inconvenient. Such classes are known every bit Inner class as well as they are ever associated alongside an event of outer class.




Nested Static Class inward Java

H5N1 nested class is truly a fellow member of a top-level class, therefore it's non much departure alongside a static variable. All instances of the top-level class volition convey a reference of same nested class if its static, otherwise each of them volition refer to a different event of nested class.

One of the primary advantages of making a nested class static is how y'all instantiate it. Suppose y'all declare a nested class B within a overstep score class A, as well as therefore it would hold upwards referred every bit A.B as well as y'all tin post away create an event of this class every bit A.B nestedStaticInstance = novel A.B(), different Testing.B bs = novel Testing(). novel B(); for creating an event of a non-static class, every bit good known every bit inner class.

So, Yes, you tin post away declare a class static inward Java, provided the class is within a overstep score class. Such classes are every bit good known every bit nested classes as well as they tin post away hold upwards declared static, precisely if y'all are thinking to brand a overstep score class static inward Java, as well as therefore it's non allowed. If y'all do so, the compiler volition complain saying "illegal modifier for the class, alone public, abstract as well as concluding are permitted".


Now, let's encounter to a greater extent than or less code inward activity to test our point. First, let's drive to declare a overstep score class static inward Java as well as encounter if nosotros tin post away do it or no.

 depending on whether y'all are talking almost a overstep score class or a nested class inward Java Can nosotros declare a class Static inward Java?


You tin post away encounter the compile-time error, it agency it is illegal to role the static modifier alongside a overstep score class inward Java. It doesn't affair whether the class is public or package as well as whether its refer is same every bit the refer of the Java beginning file. In short, y'all cannot brand a overstep score class static inward Java i.e. the class which is non within to a greater extent than or less other class. Here is to a greater extent than or less other example, where I convey tried to brand a non-public precisely overstep score class static inward Java:

 depending on whether y'all are talking almost a overstep score class or a nested class inward Java Can nosotros declare a class Static inward Java?


You tin post away see, still, the same compile fourth dimension error, "illegal modifier for the class, alone public, abstract as well as concluding are permitted" is thrown".  If y'all are hearing almost nested or inner class get-go time, You should get-go read a practiced center Java mass e.g. Head First Java to acquire to a greater extent than almost nested class inward Java.

 depending on whether y'all are talking almost a overstep score class or a nested class inward Java Can nosotros declare a class Static inward Java?


Now, let's drive to brand a nested class i.e. a class within the overstep score class static inward Java. As per theory, y'all tin post away declare a nested class static inward Java, let's encounter that inward code.

This fourth dimension at that spot is no compiler error, it means you tin post away brand a nested class static inward Java. I convey every bit good shown y'all how y'all tin post away create an event of the nested static class inward Java. You tin post away see, y'all tin post away create an event without creating an event of the outer class which was non possible with non-static nested class or inner class.


 depending on whether y'all are talking almost a overstep score class or a nested class inward Java Can nosotros declare a class Static inward Java?



That's all almost whether y'all tin post away declare a class static inward Java or not. Of course, y'all cannot brand a overstep score class static inward Java precisely y'all tin post away ever brand a nested class static inward Java. In fact, it is ane of the best do as well as every bit good suggested inward Effective Java to prefer nested static class instead of non-static inner class.

Other Interesting questions for Java developers
  • Can y'all overload or override static method inward Java? (answer)
  • Can y'all override a someone method inward Java? (answer)
  • Can y'all run a Java programme without the main() method? (answer)
  • Can y'all brand an array volatile inward Java? (answer)
  • Can y'all overload or override the primary method inward Java? (answer)
  • Can an Abstract class convey a constructor inward Java? (answer)
  • Java Programming Interview Exposed past times Markham (book)

Thanks for reading this article therefore far, if y'all truly similar this article as well as therefore delight part alongside your friends as well as colleagues. If y'all convey whatsoever question, feedback or proposition as well as therefore delight drib a comment as well as I'll drive to answer.

Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming inward Java
Java - Object Oriented Programming [For Absolute Beginners]

Komentar

Postingan populer dari blog ini

Difference Betwixt Struts Validatorform Vs Validatoractionform - Interview Question

How To Convert Inputstream To Byte Array Inwards Coffee - Two Examples

Difference Betwixt Fileinputstream Together With Filereader Inwards Coffee | Inputstream Vs Reader