Anda di halaman 1dari 3

Note :-Nested class = Static inner class= inner class Enclosing class= Outer Class= Parent Class=Top Level

Class Example: class Outer { class Inner { } }

===>Nested classes are simply static inner classes. ==>The difference between nested classes and inner classes is the same as the difference between static and nonstatic members of a cl ass: ===>Nested classes are associated with the enclosing class itself, ===>whereas inner classes are associated with an object of the enclosing class. ===>In addition, nested classes have access to all members of the enclosing clas s.

when to use inner classes :- ???? ==>A class within a class is called as an inner class. ==> In java we need the code to be reusable and flexible to maintain.

==> A class certainly contains a code that needs to communicate with its member s, but, inorder to communicate with the members that share the common proper ties we need the inner classes. ==> If we put the inner classes the encapsulation (protective layer ) is given to the nested classes within the enclosing class.

Static Inner Class :==>We can simply identify a Static Inner Class as it's name is preceeded by "static" keyword. ==> We can also provide access specifiers to a Static class as well as to it's members(code & data) . When to use Static class:static methods :- methods within a static class. static members:- members of a static class. ===>Static classes accepts only static members & static methods .

==> These static methods are executed without an object. => but , if an object is created it occupies some m emory in heapspace =>As static methods doesnot require objects , they don ot consume memory. Permissions :==>static classes and methods can communicate only with the other sta tic families. ==>static class members can be accessed directly without any reference . = =>they are just accessible with a (.) dot operator. ===> but all the other classes can access the static class and its memb ers(code , data) & methods. ==> static members can be accessed directly with their names. example:static.static class membername =>they are also accessed as follows Outerclass.static inner class Outerclass.static innerclass inner Object= new Outerclass.static innerclass( ); =>static classes are most efficient for saving memory and hence they are used if their necessity is only for a less er use & short period . Example:Note:- The best example is main( ) public static void main(String[] args) Local classes:==> These local classes are also inner classes ==> they are usually defined with in a block of code Permissions:==> they can access the all other classes of the enclosing class. ==> we usually find them within the body of a method. ==> if you declare a local class within a method , then it can access the method parameters [ Java SE8 ] ==> local classes has access to local variables which are declared as f inal or effectively final . When to use Local Classes:==> when access to a constructor is neede d .

==>When we want to invoke additional meth ods . ==> when we need to create more objects o f the class.

Nested classes :Note:Nested class=inner class ===> Nested classes may be static or Non-static. ===> One or more number of inner class within a outer class are often c alled as Nested class. ==>Nested classes are almost needed when the requirements are as same as the local classes. ==>They can also access the the non-public members of its enclosing (Oute r) class. ===> if different inner classes are made within an outer class , =>the classes will get some encapsulation(protectio n layer) =>the code will be easily maintained and is flexible for further enhancements.

Anonymous classes:=> Anonymous classes are classes without names, Usually we need to specify a constructo r with its class name. and hence they are not eligible to m ake a constructor

Anda mungkin juga menyukai