Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


ravindra
Java Inner Class
1 Answer(s)      6 years and 7 months ago
Posted in : Java Interview Questions


How many classes we can create in an inner class?


Advertisement
View Answers

February 26, 2008 at 6:35 PM


How many classes we can create in an inner class

ANS :- Within Main class{} we can create any number of the inner classes according to the requirement there is no limit .

In Java programming language, when a class is defined within another class then such a class is called a nested class. Nested classes are a feature of Java that is included in jdk1.1.

The reasons of why we use nested classes are:

It is a way of logically grouping classes that are only used in one place.
It increases encapsulation.
Nested classes can lead to more readable and maintainable code.
These classes are divided into two categories: static and non-static.

Nested classes that are declared static are simply called static nested classes. A static class has no access to instance-specific data.

Non-static nested classes are called inner classes. It has access to all of its enclosing class's instance data, including private fields and methods.

Nested classes are associated with the enclosing class itself, whereas inner classes are associated with an object of the enclosing class.

The given class structure shows the way of using the nested class.

Java Inner Class Example

class OuterClass {

...

static class StaticNestedClass {

...

}

class InnerClass {

...

}

}

public class DataStructure {
//create an array
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];

public DataStructure() {
//fill the array with ascending integer values
for (int i = 0; i < SIZE; i++) {
arrayOfInts[i] = i;
}
}

public void printEven() {
//print out values of even indices of the array
InnerEvenIterator iterator = this.new InnerEvenIterator();
while (iterator.hasNext()) {
System.out.println(iterator.getNext() + " ");
}
}

//inner class implements the Iterator pattern
private class InnerEvenIterator {
//start stepping through the array from the beginning
private int next = 0;

public boolean hasNext() {
//check if a current element is the last in the array
return (next <= SIZE - 1);
}

public int getNext() {
//record a value of an even index of the array
int retValue = arrayOfInts[next];
//get the next even element
next += 2;
return retValue;
}
}

public static void main(String s[]) {
//fill the array with integer values and print out only values of even indices
DataStructure ds = new DataStructure();
ds.printEven();
}
}



Related Tutorials/Questions & Answers:
Java inner class
Java inner class  What are inner class and anonymous class
Inner Class - Java Beginners
Inner Class  Hi, I have the following question. class Outer{ class Inner{ } public static void main(String[] args) { Outer.Inner... modifier of Inner class as "private" i.e., private class Inner{ } then after
Advertisements
Java inner class and static nested class
Java inner class and static nested class  Java inner class and static nested class
java inner class - Java Beginners
java inner class  What is the difference between the nested class and inner class? Explain with suitable example.  Hi Friend... or within a static context of that class wheras Inner class is a nonstatic class
Inner class in java
Inner class in java In this example we will describe inner classes in java. Inner class declared inside a class. The inner class also call Nested classes. Inner classes provides an graceful and powerful feature to the Java. Inner
inner class - Java Interview Questions
Java Inner Class  How many classes we can create in an inner class?  How many classes we can create in an inner class ANS :- Within Main... class structure shows the way of using the nested class. Java Inner Class
Inner class - Java Interview Questions
Inner class  whether inner class can implement any interface(Runnable... Inner inner; private class Inner extends Thread { Inner(String name...(name); } } // Using an anonymous inner class: class InnerClass2
Inner class
Inner class  when we use inner classed in java
What is inner class in Java? - Java Beginners
What is inner class in Java?  Hi, Explain me inner classes in Java. What is the use of inner classes? Give examples of inner classes in Java...://www.roseindia.net/java/master-java/inner.shtml Uses: Non-static inner class
inner class
inner class  what is mean by inner class   Inner class is a non-static class which is defined inside another class
Inner class
{ i=new Inner(); } class Inner...("Inner"); } } } class...Inner class   //This code doesn't Works// import
Inner class
Inner class  how can i call outer class A `s method to print Outer. class A { void m() { System.out.println("outer"); } } public...(); class A { void m() { System.out.println("inner"); } } } class A { void m
Inner class in java
, a non-static nested class is actually associated to an object rather than to the class in which it is nested. For more details click on the following link Inner class in Java
Use of Local Inner class
. The inner class Get Even Integer is same as Java iterators,The work of Get Even... Use of Local Inner class       The Java language enables you to define a class inside another
about inner class
about inner class  class Outer { class Inner{ public void m1(){ System.out.println("Inner class Method"); } } public void m2(){ Inner i = new Inner...() class Outer please tell me, if any coding error and tell me the resolution
new operator for creating the object of inner class
new operator for creating the object of inner class  Hi , The folliowing is my code : class Outer { class Inner{ void show... "); } } } class Test { public statis void main(String args
What is the difference between a static and a non-static inner class?
What is the difference between a static and a non-static inner class?   Hi, What is the difference between a static and a non-static inner class? Thanks
How are Anonymous (inner) classes used in Java?
How are Anonymous (inner) classes used in Java?  How are Anonymous (inner) classes used in Java
Inner Classes In Java
Inner Classes In Java      ... and powerful feature to the Java programming language.  -- These inner... as a non-static member of an enclosing class. This type of inner class
Inner classes
Inner classes   Hi I am bharat . I am student learning java... there is Inner class defined . A) Outer ob = new Outer(); Ob.display(); // calling instance method of Outer class Outer.Inner ob1= ob.new Inner(); // creating
Member Classes, Inner Nested Classes in java
in the middle of an expression which is allows us by an inner class The syntax...;  Member classes are defined within the body of a class. We can use member classes anywhere within the body of the containing class. We declare member classes
Nested and Inner classes
a nested class. Inner class is a non static class declared inside another class.Non-static inner class keep the reference of outer class and allow to access member...Nested and Inner classes  What is the significance of Inner Classes
Anonymous Inner Classes - Anonymous Inner Classes tutorial
the inner class, there are two types of supplementary  inner classes : The inner class which is declared inside the body of a method is known as the local inner classes. The class declared inside the body of a method without naming
Java nested class example
Java nested class example  Give me any example of Nested Class.  ... nested class is declared static. Non-static nested class is also called inner... InnerClass{ private String inner = "Inner Class"; //InnerClass instance
Java class
Java class  What is the purpose of the Runtime class
java class
java class  write a java program to display a msg "window closing" when the user attempts to close the window.(a) create a window class (b) create frame within the window class (c) extends window adapter class to display the msg
java class
java class  please send me the answer of the question which i have send according of java class assoon as possible . Write a java program to display.... a. Create a window class. b. Create frame within the window class. c. Extend
SQL INNER Joins
_name, s.stu_class, l.lib_no from stu_table as s inner JOIN lib_table as l... SQL INNER Joins      .... Understand with Example The Tutorial illustrate an example from SQL INNER
Java Nested Class
Java Nested Class         In Java programming language, when a class is defined within another.... Nested classes are associated with the enclosing class itself, whereas inner classes
java class - Java Beginners
java class  How to run a java class(Mail.java) automatically in tomcat server for a particular time
The link to the outer class,java tutorial,java tutorials
Nested Classes: Inner & Outer Class The classes which are defined within...; as Inner Class. The Class in which inner class resides are known as Outer Class. Inner Class  has access to the private member of the outer class
SCJP Module-3 Question-2
Given a sample code: 1 public class Test { 2 public static void main(String... i.innerMethod(); 5 OuterClass o = new OuterClass(); 6 o.x=5; }} 7 class OuterClass { 8 public int x = 9; 9 public OuterClass() { 10 InnerClass inner = new
Java Class Size
Java Class Size  How to determine size of java class
converting java class to java fx class
converting java class to java fx class   i am working on a java project which would convert text to speech and speech to text. i have developed a java file that would work for text to speech. it is working fine as a .java file
class method in java
class method in java  How to write a class method in Java?   You must read Java - Class, Object and Methods in Java
Java dictionary class
Java dictionary class  What is the Dictionary class
Java adapter class
Java adapter class  What is adapter class
java class - Java Beginners
java class  Define a class product with the following data members 1.Product number int(Auto generated) 2.Product name char [20] 3.Price float... program to execute the above class ad functions for 5 products
Example of HashMap class in java
Example of HashMap class in java. The HashMap is a class in java collection framwork. It stores values in the form of key/value pair. It is not synchronized
Java class file
Java class file  How do i open a java class file using eclipse
java Class - Java Beginners
java Class  Can anyone please explain what this code means? import java.util.TreeMap; import java.util.Iterator; //Please tell me what this class declaration means???????????? public class ST, Val> implements Iterable
Java System class
Java System class  What is the purpose of the System class
abstact class - Java Beginners
abstact class  write a java program to implement an animal abstact class
Java math class
Java math class  Can you instantiate the Math class
java Math class
java Math class  Why are the methods of the Math class static
Java file class
Java file class  What is the purpose of the File class
Java abstract class
Java abstract class  Can an abstract class be final
Hibernate Inner Join
In this section, you will learn how to do Inner Join in Hibernate
java anonymous class
java anonymous class  Can an anonymous class be declared as implementing an interface and extending a class
Java Interview Questions - Page 6
a growable array of objects What modifiers may be used with an inner class that is a member of an outer class? A (non-local) inner class may be declared... Java Interview Questions - Page 6   
 

 

 

DMCA.com