• No results found

Static nested classes:

In document Core_Java By Venu Kumaar (Page 165-171)

Inner classes

4) Static nested classes:

Some times we can declare inner class with static modifier, such type of inner classes are called

"static nested classes".

In the normal inner class, inner class object always associated with outer class object. i.e., without existing outer class object there is no chance of existing inner class object.

But, static nested class object is not associated with outer class object i.e., without existing outer class object there may be a chance of existing static nested nested class object.

Ex:

class Outer {

static class Nested { public void m1() {

System.out.println("Static Nested class Method");

} }

public static void main(String args[]) { Outer.Nested n = new Outer.Nested();

n.m1();

} }

/*OUTPUT:

Static Nested class Method

*/

With in the static nested class we can declare static members including main() also. Hence,it is possible to invoke nested class directly from command prompt.

class Outer {

static class Nested {

public static void main(String args[]) {

System.out.println("Static Nested class main() Method");

}

||Om Shree Hanumathe Namaha||

Venu Kumaar.S

}

public static void main(String args[]) {

System.out.println("Outer class main() Method");

} }

javac Outer.java java Outer Output:

Outer class main() Method java Outer$Nested

Output:

Static Nested class main() Method

Normal Inner Class Vs Static Nested Class : Inner class:

1)Inner class object is always associated with Outer class object. i.e., without existing Outer class object there is no chance of existing Inner class object.

2) Inside normal Inner class we can't declare static members.

3) Inside normal Inner class we can't declare main() and hence it is not possible to invoke inner class directly from command prompt.

Static Nested Class:

1)Static Nested class object is not associated with Outer class object. i.e.,with out existing Outer class object there is a chance of existing Static Nested class object.

2) Inside normal Static Nested class we can declare static members.

3) Inside normal Static Nested class we can declare main() and hence it is possible to invoke inner class directly from command prompt.

General class Vs Anonymous Inner Class:

A General class can extend only one class at a time.

Where as Anonymous Inner class also can extend only one class at time.

A General class can implement any number of Interfaces.

Where as Anonymous Inner class can implement only one interface at time.

A General class can extend another class & can implement an interface simultaneously.

Where as Anonymous Inner class can extend another class or can implement an interface but not both simultaneously.

enum

enum: Every enum in java is direct child class of "java.lang.Enum" always every enum is "final",

because of this we can conclude inheritance concept is not applicable for enums explicitly. But enum can impl any number of interfaces at a time.

||Om Shree Hanumathe Namaha||

Venu Kumaar.S

invalid: calss x{} enum y extends x{}, enum x{}class y extends x{},enum x{}, enum y extends x, enum x extends java.lang.Enum {}

CTE:Cannot inherit from final x enum types not extensible

valid: interface x{} enum y implements x{}

java.lang.enum: Is an abstract class and direct child class of Object class.

This class implements Comparable & Serializable interfaces. Hence every enum in java is by default Serializable and Comparable.

values(): This method is used to list out all values of enum.

Beer[] b=Beer.values();

ordinal(): Using this method we can find ordinal value of enum constant. ordinal value is the order value of enum constants

We can declare enum either with in the class or outside of the class but not inside a method.

If we are trying to declare enum with in a method we will get CTE

If we declare enum outside the class the applicable modifiers are public, default, strictfp.

If we declare enum with in a class the applicable modifiers are public,default,strictfp,private,protected, static.

From 1.5v onwards we can use enum as arg to switch statement.

switch(arg) args 1.4v

byte, short, char, int

1.5v

Byte, Short, Character, Integer, enum 1.7v

String

/**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:EnumTest

.java

*/

enum Beer {

KF, RC, BW, KO;

}

class EnumTest {

public static void main(String args[]) { Beer b1 = Beer.RC;

switch (b1) { case KF:

System.out.println("King Fisher");

break;

case RC:

System.out.println("Royal Challenge");

break;

case KO:

System.out.println("KnockOut");

break;

case BW:

System.out.println("BudWiser");

break;

||Om Shree Hanumathe Namaha||

Venu Kumaar.S default:

System.out.println("Other Brands not recommanded");

}

java.lang.enum: Is an abstract class and direct child class of Object class.

This class implements Comparable & Serializable interfaces. Hence every enum in java is by default Serializable and Comparable.

values(): This method is used to list out all values of enum.

Beer[] b = Beer.values();

ordinal(): using this method we can find ordinal value of enum constant. ordinal value is the order value of enum constants

/**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:EnumTest1

.java

*/

enum Beer {

KF, RC, BW, KO;

}

class EnumTest1 {

public static void main(String args[]) { Beer[] b = Beer.values();

for (Beer b1 : b) {

System.out.println(b1 + "---" + b1.ordinal());

}

Inside enum we can declare main() & hence, we can invoke enum class directly from command prompt.

In addition to constant if we want to take any extra members compulsory list of constants should be in the 1st line & should ends with ";".

ex: enum Colour {

RED, GREEN, BLUE;

||Om Shree Hanumathe Namaha||

Venu Kumaar.S public void m1() {

} }

Inside enum without having constant we can't to take any extra members, but empty enum is always valid.

invalid: enum Colour {

public void m1() {

}

}

valid : enum Colour { }

/**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:EnumTest2

.java

*/

enum EnumTest2 { KF, RC, BW, KO;

public static void main(String args[]) {

System.out.println("ENUM main method...");

EnumTest2[] b = EnumTest2.values();

for (EnumTest2 b1 : b) {

System.out.println(b1 + "---" + b1.ordinal());

} }

} /*

Output:

java EnumTest2 ENUM main method...

KF---0 RC---1 BW---2 KO---3

*/

enum class constructors:

with-in enum we can take constructors also.

enum class constructors will be executed automatically at the time of enum class loading, because enum constants will be created at the time of class loading only.

we can't invoke enum constructors explicitly. we can't create Objects of enum explicitly & hence we can't call constructors directly. If we try it shows CTE

Beer b = new Beer(); CTE: enum types may not be instantiated.

/**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:EnumTest3

.java

||Om Shree Hanumathe Namaha||

Venu Kumaar.S

class EnumTest3 {

public static void main(String args[]) { System.out.println("main method...");

Beer b = Beer.KF;

System.out.println(b + "---" + b.ordinal());

}

Within the enum we can take instance & static methods, but we can't to take abstract methods.

Every enum constant represents an Object hence whatever the methods we can apply on normal java Object we can apply those on enum constants also.

enum: It is a keyword which can be used to define a group of named constants.

Enum: It is a class present in "java.lang" pkg which acts as a base class for all java enums.

Enumeration: It is an Interface present in "java.util" pkg, which can be used for retrieving objects from Collection one by one.

/**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:EnumTest4

.java

*/

||Om Shree Hanumathe Namaha||

Venu Kumaar.S

}

class EnumTest4 {

public static void main(String args[]) { Colour[] c = Colour.values();

for (Colour c1 : c) { c1.info();

} }

} /*

Output:

java EnumTest4 Universal Colour...

Dangerous Colour...

Universal Colour...

*/

Thread

Multitasking:

Executing several tasks simultaneously is called "Multitasking".

There are two types of multitasking.

In document Core_Java By Venu Kumaar (Page 165-171)