Java Inner Class

 Inner class:

1.   Nested inner class

2.   Local inner class

3.   Anonymous inner class

4.   Static inner class.


Nested Inner class:

A class inside a class is being called.

Example 01:

package Classes;

// Nested Inner Class

class outer {

int a=50;

class inner

{

int b=58;

void innerDisplay()

{

System.out.println("A : "+a);

System.out.println("B : "+b);

}

}

void outerDisplay()

{

inner i =new inner();

i.innerDisplay();

System.out.println("B from inner Class by Outer Display : "+i.b);

}

}

public class nestedClass {

public static void main(String args[]) {

outer o =new outer();

o. outerDisplay();

outer.inner i =new outer().new inner();

i.innerDisplay();

}

}

/*A class is a non-primitive or user-defined data type in Java,

* while an object is an instance of a class.

* To define a class within another class.

* Such a class is called a Nested Class.

* Nested Classes are called Inner Classes if they were declared as non-static,

* if not, they are simply called Static Nested Classes.

* This page is to document and provide details with examples on how to use Java Nested and Inner Classes.


Syntax:

class Class_Name // OuterClass

{

. . .

class Class_Name // NestedClass

{

. . .

}

}*/

Local inner class:

  • A class inside a method
  •  A class is a non-primitive or user-defined data type in Java,
  •  while an object is an instance of a class. Local classes are classes that are defined in a block,
  •  which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.
  • This section covers the following topics: Declaring Local Classes.
  • Accessing Members of an Enclosing Class. A class i.e. created inside a method is called a local inner class in Java.
  • If you want to invoke the methods of the local inner class, you must instantiate this class inside the method.

 Syntax:

   class Class_Name   // OuterClass

   {

      void method ( )

      {

          class Class_Name  // NestedClass

          {

           . . .

          }

      }

}

Example 02:

package Classes;

//Local Inner Class

class Outercls

{

void display()

{

class Inner

{

void innerDisplay()

{

System.out.println("Inner Display");

}

}

Inner i =new Inner();

i.innerDisplay();

}

}

public class localInnerClass {

public static void main(String[] args) {

Outercls o =new Outercls();

o.display();

}

}


/*

A class is a non-primitive or user-defined data type in Java,

while an object is an instance of a class. Local classes are classes that are defined in a block,

which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.


This section covers the following topics: Declaring Local Classes.

Accessing Members of an Enclosing Class. A class i.e. created inside a method is called local inner class in java.

If you want to invoke the methods of local inner class, you must instantiate this class inside the method.


Syntax:

class Class_Name // OuterClass

{

void method ( )

{

class Class_Name // NestedClass

{

. . .

}

}

}*/


ANONYMOUS INNER CLASS:

  • A class is a non-primitive or user-defined data type in Java, while an object is an instance of a class.
  •  * To define a class within another class. Such a class is called a Nested Class. An anonymous it is an inner class without a name and
  •  * for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.
Example 03:

package Classess;

//Anonymous Inner Class

abstract class testDemo {

abstract void display();

}


class outerDemo {

public void outerDisplay() {

testDemo o =new testDemo() {

@Override

public void display() {

System.out.println("Test Display");

}

};

o.display();

}

}

public class anonymousInner {

public static void main(String[] args) {

outerDemo o =new outerDemo();

o.outerDisplay();

}

}

/*A class is a non-primitive or user-defined data type in Java, while an object is an instance of a class.

* To define a class within another class. Such a class is called a Nested Class. An anonymous it is an inner class without a name and

* for which only a single object is created. An anonymous inner class can be useful when making an

* instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.*/


STATIC INNER CLASS:

  • The static keyword is used on a class, method, or field to make it work independently of any instance of the class.
  •  * Static fields are common to all instances of a class. They do not need an instance to access them.
  • Static classes can be declared inside of other classes. They do not need an instance of the class they are in to be instantiated.
Example 04:

package Classess;

//Static Inner Class

class OuterClass {

static int x=10;

int y=20;

static class InnerClass

{

void display()

{

System.out.println("X : "+x);

}

}

}

public class staticInnerClass {

public static void main(String[] args) {

OuterClass.InnerClass i =new OuterClass.InnerClass();

i.display();

}

}


/*The static keyword is used on a class, method, or

field to make them work independently of any instance of the class.

* Static fields are common to all instances of a class.

They do not need an instance to access them.



Static methods can be run without an instance of the class they are in.

However, they can only access static fields of that class.

Static classes can be declared inside of other classes.

They do not need an instance of the class they are in to be instantiated.*/

STATIC VARIABLES:

if we prefix any variable ‘static’ with the keyword it would become a static variable.

Common for all objects.

Static methods can be run without an instance of the class they are in.

However, they can only access static fields of that class.

Example 05:

package Classess;


//Static Variables and Static Methods


class staticTest

{

static int a=10;

int b=20;


void show()

{

System.out.println("A : "+a+" B : "+b);

}

static void display()

{

System.out.println("A : "+a);

}

}


public class stat_vari_methods {


public static void main(String args[])

{

staticTest o1=new staticTest();

o1.show();

staticTest o2=new staticTest();

o2.b=100;

staticTest.a=200;

o2.show();

o1.show();

}

}


/*Static method in Java is a method which belongs to the class and

not to the object.

* A static method can access only static data. It is a method

which belongs to the class and not to the object(instance).


A static method can access only static data. It cannot access

non-static data ( instance variables ).

A static method can call only other static methods and cannot call

a non-static method from it.

A static method can be accessed directly by the class name and

doesn’t need any object .*/


STATIC METHODS:

If we prefix any method with the ‘static’ keyword it would become a static method.

we can only use static variables inside static methods.

Example 06:

package Classes;


//Static Variables and Static Methods


class staticTest

{

static int a=10;

int b=20;


void show()

{

System.out.println("A : "+a+" B : "+b);

}

static void display()

{

System.out.println("A : "+a);

}

}


public class stat_vari_methods {


public static void main(String args[])

{

staticTest o1=new staticTest();

o1.show();

staticTest o2=new staticTest();

o2.b=100;

staticTest.a=200;

o2.show();

o1.show();

}

}


/*Static method in Java is a method that belongs

to the class and not to the object.

* A static method can access only static data.

It is a method that belongs to the class and not to the object(instance).


A static method can access only static data. It cannot access

non-static data ( instance variables ).

A static method can call only other static methods and

cannot call a non-static method from it.

A static method can be accessed directly by the class name and

doesn’t need any object .*/



STATIC BLOCKS:

They are the blocks that will execute before our main block.

Example 07:

package Methods;

//Static Blocks in Java


class BlockTest

{


static {

System.out.println("BlockTest-1");

}

static {

System.out.println("BlockTest-2");

}

}


public class staticBlocks {


static {

System.out.println("Block-1");

}

public static void main(String[] args) {

BlockTest o =new BlockTest();

System.out.println("Main");

}

static {

System.out.println("Block-2");

}

}


/*The static block is a set of instructions that is run only once when a class is loaded into memory.

* A static block is also called a static initialization block.

* This is because it is an option for initializing or setting up the class at run-time.

*/




Post a Comment

0 Comments