Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 01-02-2010
Member
 
Join Date: Nov 2009
Posts: 37
How to create an inner class in java?

Hi friends,
I am last year Computer Science student. I recently started learning java language. In our last lecture our sir taught us "inner class in java", but I still don't understand this concept clearly. I want example of it. Can anyone tell me how to create an inner class in java? Please help me.
Reply With Quote
  #2  
Old 01-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: How to create an inner class in java?

An inner class is a class that defined inside another class. Inner classes is used to constructed in several contexts. An inner class defined anywhere in that class. When you defined an inner class defined inside any method, then that class can be referred to in the same method.In the following example "InnerClassEg" is the Inner class.

Code:
package org.kodejava.example.lang;

public class InnerClassEg {
    private Bean beans;

    class Bean {
        public int widths;
        public int heights;

        @Override
        public String toString() {
            return widths + "y " + heights;
        }
    }

    public InnerClassDemo() {
        Bean beans = new Bean();
        beans.widths = 300;
        beans.heights = 510;

        this.beans = beans;
    }

    public Bean getBeand() {
        return this.beans;
    }

    public static void main(String[] args) {
        InnerClassEg inners = new InnerClassEg();
        System.out.println("inner.getBeadn() = " + innerd.getBeand());
    }
}
Reply With Quote
  #3  
Old 01-02-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: How to create an inner class in java?

In the following example DataStr is outer class which is used to includes methods to add an integer onto the array and used to print out values of even indices of the array. In the following example InnerEvenIterator is inner class. InnerEvenIterator is similar to a standard Java iterator. I have written following program for you, where I have create inner class.

Code:
public class DataStr {
    
    private final static int S = 15;
    private int[] arrayOfInts = new int[S];
    
    public DataStructure() {
       
        for (int k = 0; k < S; k++) {
            arrayOfInts[k] = k;
        }
    }
    
    public void printEven() {
      
        InnerEvenIterator iterators = this.new InnerEvenIterator();
        while (iterators.hasNext()) {
            System.out.println(iterators.getNext() + " ");
        }
    }
    

    private class InnerEvenIterator {
  
        private int nexts = 0;
        
        public boolean hasNext() {
          
            return (nexts <= S - 1);
        }
        
        public int getNext() {
     
            int retValues = arrayOfInts[nexts];
           
            nexts += 2;
            return retValues;
        }
    }
    
    public static void main(String s[]) {

        DataStructure dss = new DataStructures();
        dss.printEven();
    }
}
Reply With Quote
  #4  
Old 01-02-2010
Member
 
Join Date: May 2008
Posts: 2,383
Re: How to create an inner class in java?

In the following program I have use "Bases" as outer class and "AnonymousCos" as inner class. It helps constructor to define any method. It is very easy process to create an inner class in java. Just try to understand following example. It is very simple.

Code:
abstract class Bases {
  public Base(int k) {
    System.out.println("Base constructors, k = " + k);
  }

  public abstract void fs();
}

public class AnonymousCos {

  public static Base getBase(int k) {
    return new Bases(k) {
      {
        System.out.println("Inside another method");
      }

      public void fs() {
        System.out.println("In anonymous fs()");
      }
    };
  }

  public static void main(String[] args) {
    Base bases = getBase(32);
    bases.fs();
  }
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 01-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: How to create an inner class in java?

Inner classes are type non-static classes which are defined into another classes. Following is the syntax for defining inner class in java.

class Outer {
class Inner {}

}

After compiling the program the compiled class files for the above are: Outer.class and Outer$Inner.class. The Inner class type can be defined as: Outer.Inner. It is possible to create instances of inner classes can be created in a number of ways .
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to create an inner class in java?"
Thread Thread Starter Forum Replies Last Post
Converting java class to java bean class Ucchal Software Development 6 09-08-2010 10:24 AM
How to create a class in java script Lawford Software Development 5 11-02-2010 02:16 AM
How to create mouse event using Robot class in java? Luis-Fernando Software Development 4 03-02-2010 08:03 PM
How to create key press event using Robot class in java? Kasper Software Development 4 03-02-2010 06:33 PM
Java Programming Language Basics: Reflection Basics and Class Class mayuri_gunjan Guides & Tutorials 6 29-08-2005 12:04 AM


All times are GMT +5.5. The time now is 06:21 PM.