Tuesday, 21 January 2014

INTRODUCING NESTED AND INNER CLASSES

NESTED CLASSES IN JAVA

A class declared inside a class is known as nested class. We use nested classes to logically group classes in one place so that it can be more readable and maintainable code. Moreover, it can access all the members of outer class including private members.


Advantage of nested classes

There are basically three advantages of nested classes. They are,

  • Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
  • Nested classes can lead to more readable and maintainable code because it logically group classes in one place only.
  • Code Optimization as we need less code to write.


Syntax of Nested class:

class Outer_class_Name
{  
 ...  
 class Nested_class_Name
{  
  ...  
 }  
 ...  
}  

Types of Nested class:

There are two types of nested classes non-static and static nested classes.

1.non-static nested class(inner class)
              a)Member inner class
              b)Annomynous inner class
              c)Local inner class
2.static nested class

1.non-static nested class(inner class):

A class that is declared inside a class but outside a method is known as member inner class.The non-static nested classes are also known as inner classes.

Invocation of Member Inner class
  1. From within the class
  2. From outside the class

Example of member inner class that is invoked inside a class:

In this example, we are invoking the method of member inner class from the display method of Outer class.

class Outer
{  
 private int data=30;  
 class Inner{  
  void msg(){System.out.println("data is "+data);
}  
 }  
   
 void display(){  
  Inner in=new Inner();  
  in.msg();  
 }  
 public static void main(String args[])
{  
  Outer obj=new Outer();  
  obj.display();  
 }  
}  
Output:data is 30

2.Static nested class:

A static class that is created inside a class is known as static nested class. It cannot access the non-static members.It can access static data members of outer class including private.static nested class cannot access non-static (instance) data member or method.

Program of static nested class that have instance method:

class Outer
{
  static int data=90;

  static class Inner
{
   void msg(){System.out.println("data is "+data);
}
  }
   public static void main(String args[])
{
  Outer.Inner obj=new Outer.Inner();
  obj.msg();
  }
}  

Output:
data is 90






8 comments :

  1. wow , great stuff , thanks for sharing
    redhat certification in chennai | rhce training in chennai | redhat training in chennai

    ReplyDelete
    Replies
    1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

      Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.

      Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

      The Nodejs Projects Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Java inner class is most important question in any java interview.
    thanks of sharing this information.

    ReplyDelete
  3. instagram takipçi satın al - instagram takipçi satın al - tiktok takipçi satın al - instagram takipçi satın al - instagram beğeni satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - instagram beğeni satın al - instagram beğeni satın al - polen filtresi - google haritalara yer ekleme - btcturk güvenilir mi - binance hesap açma - kuşadası kiralık villa - tiktok izlenme satın al - instagram takipçi satın al - sms onay - paribu sahibi - binance sahibi - btcturk sahibi - paribu ne zaman kuruldu - binance ne zaman kuruldu - btcturk ne zaman kuruldu - youtube izlenme satın al - torrent oyun - google haritalara yer ekleme - altyapısız internet - bedava internet - no deposit bonus forex - erkek spor ayakkabı - webturkey.net - minecraft premium hesap - karfiltre.com - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word indir - misli indir

    ReplyDelete
  4. instagram takipçi satın al
    instagram takipçi satın al
    takipçi satın al
    instagram takipçi satın al
    takipçi satın al
    aşk kitapları
    tiktok takipçi satın al
    instagram beğeni satın al
    youtube abone satın al
    twitter takipçi satın al
    tiktok beğeni satın al
    tiktok izlenme satın al
    twitter takipçi satın al
    tiktok takipçi satın al
    youtube abone satın al
    tiktok beğeni satın al
    instagram beğeni satın al
    trend topic satın al
    trend topic satın al
    youtube abone satın al
    beğeni satın al
    tiktok izlenme satın al
    sms onay
    youtube izlenme satın al
    tiktok beğeni satın al
    sms onay
    sms onay
    perde modelleri
    instagram takipçi satın al
    takipçi satın al
    tiktok jeton hilesi
    pubg uc satın al
    sultanbet
    marsbahis
    betboo
    betboo
    betboo

    ReplyDelete
  5. mmorpg oyunlar
    INSTAGRAM TAKİPÇİ SATİN AL
    Tiktok Jeton Hilesi
    tiktok jeton hilesi
    antalya saç ekimi
    instagram takipçi satın al
    metin2 pvp serverlar
    instagram takipçi satın al
    INSTAGRAM TAKİPCİ SATIN AL

    ReplyDelete