Results 1 to 5 of 5

Thread: How to create an inner class in java?

  1. #1
    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.

  2. #2
    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());
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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();
        }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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();
      }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    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 .

Similar Threads

  1. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  2. How to create a class in java script
    By Lawford in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 02:16 AM
  3. How to create mouse event using Robot class in java?
    By Luis-Fernando in forum Software Development
    Replies: 4
    Last Post: 03-02-2010, 08:03 PM
  4. How to create key press event using Robot class in java?
    By Kasper in forum Software Development
    Replies: 4
    Last Post: 03-02-2010, 06:33 PM
  5. Java Programming Language Basics: Reflection Basics and Class Class
    By mayuri_gunjan in forum Guides & Tutorials
    Replies: 6
    Last Post: 29-08-2005, 12:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,016,423.40462 seconds with 17 queries