Java nested class example

Java nested class example

Give me any example of Nested Class.

View Answers

May 17, 2012 at 5:48 PM

Nested Class: Class defined within another class is called nested class. Nested class is divided into two parts-

1.Static

2.Non-static Static nested class is declared static. Non-static nested class is also called inner class.

Example:

public class NestedClass{
    private String outer = "Outer Class"; //NestedClass instance variable
    InnerClass innerClass = new InnerClass();
    void getOuterS(){
        System.out.println(outer); 
    }
    void getInnerS(){
        System.out.println(innerClass.inner);
    }
    class InnerClass{
        private String inner = "Inner Class"; //InnerClass instance variable, uninitialized
        void getInnerS(){
            System.out.println(inner);
        }
        void getOuterS(){
            System.out.println(NestedClass.this.outer);
        }
    }
    public static void main(String[] args){
        NestedClass nestedClass = new NestedClass();
        NestedClass.InnerClass nestedInner = nestedClass.new InnerClass();//can also be new NestedClass().new InnerClass ();
        nestedClass.getOuterS();
        nestedClass.getInnerS();
        nestedInner.getInnerS();
        nestedInner.getOuterS();
    }
}

Ads









Related Tutorials/Questions & Answers:
Java nested class example
Java nested class example  Give me any example of Nested Class.   Nested Class: Class defined within another class is called nested class. Nested class is divided into two parts- 1.Static 2.Non-static Static
What is nested class in Java with Example?
. In Java a class within a class is called nested class. For example: public class...; }  } So, above is an example of Nested class in Java. The main benefits...What is nested class in Java with Examples and explanations? In Java
Advertisements
What is nested class in Java with Example?
. In Java a class within a class is called nested class. For example: public class...; }  } So, above is an example of Nested class in Java. The main benefits...What is nested class in Java with Examples and explanations? In Java
Java inner class and static nested class
Java inner class and static nested class  Java inner class and static nested class
Java Nested Class
Java Nested Class         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
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
Example of HashSet class in java
Example of HashSet class in java. In this part of tutorial, we... unique. You can not store duplicate value. Java hashset example. How.... Example of Hashset iterator method in java. Example of Hashset size() method
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... class parts of Nested Class. Inner classes are related by different names
Java Wrapper Class Example
Java Wrapper Class Example In this section we will read about how to use... to objects of that class. In Java there are eight primitive data types and each data... about how to use the wrapper classes in Java. In this example we will create
Example for a method in java which returns a class
Example for a method in java which returns a class  Hi, I want to create a 1 class,and declare some methods there.I need to method must returns a classname.when i call that particular method(which returns a class),how can i
Static Nested Classes
; A nested class that is declared static is called a static nested class... independently of any particular outer class object. A static nested class use... an object reference. A static nested class interacts with the instance
Nested classes
class looks just like any other class, except that it is nested inside another class. ( It can even contain further levels of nested classes, but you... inner class can be either static or non-static. A static nested class
Use of Local Inner class
;  The Java language enables you to define a class inside another class. Such a class is defined as Nested Class or Local Inner class. The Nested Class are classified into - 1)Static Nested Inner class are declared static called
Class Cast Exception Example in java
Class Cast Exception Example in java We are going to describe class cast exception in java. The class cast exception is a converting one data type into another data type. A Class cast exception is a thrown by java. In this example
Java Abstract Class Example
Java Abstract Class Example In this section we will read about the Abstract class. Abstract class in Java is a class which is created for abstracting... this example into two parts, in first part we will create an abstract class and we
Inner Nested Classes
of inner nested class with the help of an example. There is an inner class... nested classes, a non-static nested class is actually associated to an object rather than to the class in which it is nested. Any member
Java 8 consumer class(interface) example
How to use the Java 8 consumer class(interface)? In this example program I...); } } //Consumer implementation example class ConsumerObject implements...("Value is:"+t); } } Read more tutorials and example of Java 8
Java Bigdecimal class example
Java Bigdecimal class example       Java bigdecimal class comes under java.math library. Class... increased the usage of this class in building Business applications.  Java
Example of a class variable (static variable)
Example of a class variable (static variable)       This Java programming example will teach you how.... For example in the class StaticVariable each instance has different copy
Java example to get Object class name at runtime
Java example to get Object class name at runtime       java get Object class name In java...; by calling the method getName() we can get the name of the object class. In our example
MySQL Nested Example
MySQL Nested Example      ... of nested select query specified in Where Clause. Understand with Example The Tutorial illustrate an example from 'MySQL Nested Select'. To understand
MySQL Nested Select Example
MySQL Nested Select Example       MySQL Nested Select is select query, that is nested inside... with Example The Tutorial illustrate an example from 'MySQL Nested Select'. To understand
Nested If Statement
. Here we are providing you an example using Nested-If statement. In the example, we... Nested If Statement       In this section you will study about the Nested-if Statement in jsp
Java writer example
Java writer example       Example below demonstrates working of abstract writer class. Java writer is an abstract class build inside the java.io package.  In the example
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
The link to the outer class,java tutorial,java tutorials
Nested Classes: Inner & Outer Class The classes which are defined within... to all the members of the it's outer class. Example Given below example will give you clear idea about inner or outer classes : class Outer
No complete code for BeanNameUrlHandlerMapping With Command Class Example
No complete code for BeanNameUrlHandlerMapping With Command Class Example  There is no complete dispatcher-servlet.xml and we couldn't download the codes
Java bigdecimal shortValue example
Java bigdecimal shortValue example       In this example working of  Java bigdecimal class... data type value. In the example one bigdecimal object  mass is created. 
Java bigdecimal toEngineeringString example
Java bigdecimal toEngineeringString example       In this example working of  Java bigdecimal class toEngineeringString method  is demonstrated.  Method return
Java - Math class in Java
Java - Math class in Java       In this example you will learn about Math class. This example...;} } Download Math Class Example

Ads