answersLogoWhite

0


Best Answer

is given below

package com.test.reflection;

public class Outer

{

public static class Nested

{

public void printMesg(String body)

{

System.out.println(body);

}

}

public class Inner

{

public void printMesg(String body)

{

System.out.println(body);

}

}

}

package com.test.reflection;

import com.test.reflection.Outer.

Nested;

public class AnotherClass {

public static void main(String[] args){

try {

//Bar b=(Bar) Foo.Bar.class.getConstructors()[0].newInstance(new Foo());

//b.printMesg("hello");

Nested nested=new Nested();

nested.printMesg("nested class");

Outer.Inner inner=new Outer().new Inner();

inner.printMesg("Inner class");

//b1.printMesg("static");

} catch (IllegalArgumentException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (SecurityException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you create an instance of inner class outside outer class in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a private class?

A private class is a class that cannot be directly accesed from outside its outer class, similar to a private variable or method. This means that a private class must always be an inner class, though an inner class can be public or protected instead. For instance, the following is valid and means that any X object cannot directly access the inner Y class. public class X { private class Y{} } The following however is invalid. private class X{}


What happens when you declare class as a static in Java?

Declaring an inner class static means that class only has access to the "outer" class public and private static fields. A non-static inner class has access to the outer class's instance data. Top-level classes cannot be declared static. The advantage of a static inner class is that it doesn't need an instance of the containing class to work and it's bytecode class size is smaller for that reason - less overhead.


Can an inner class be creatd?

Yes. public class My { public class Wallet { } } //To create one My.Wallet billFolder = new My.Wallet();


Static keyword in java?

A Static method in Java is one that belongs to a class rather than an object of a class. Normal methods of a class can be invoked only by using an object of the class but a Static method can be invoked Directly. Example: public class A { ..... public static int getAge(){ .... } } public class B { ..... int age = A.getAge(); } In class B when we wanted the age value we directly called the method using the instance of the class instead of instantiating an object of the class. Tip: A static method can access only static variables. The reason is obvious. Something that is common to a class cannot refer to things that are specific to an object...


What is static inner class?

An inner class declared as static is treated as if it were a normal top-level class.

Related questions

How do you create objects for inner class inside an another inner class?

An inner class should be for private use. Only the outer class should have access and to create an instance of it. In the following example, as the question stated, the creation a Inner2, should be done by Inner1: class Outer { class Inner1 { class Inner2 {....} } } However, if Outer need to access to Inner2 via Inner1, there is something wrong with the design. The Outer class should NOT have any knowledge of Inner2.


Inner classes in java?

An inner class is a class within a class: class MyClass { class MyInnerClass { } } Inner classes in Java are normally used for things like the nodes of a linked list.


What is a private class?

A private class is a class that cannot be directly accesed from outside its outer class, similar to a private variable or method. This means that a private class must always be an inner class, though an inner class can be public or protected instead. For instance, the following is valid and means that any X object cannot directly access the inner Y class. public class X { private class Y{} } The following however is invalid. private class X{}


What happens when you declare class as a static in Java?

Declaring an inner class static means that class only has access to the "outer" class public and private static fields. A non-static inner class has access to the outer class's instance data. Top-level classes cannot be declared static. The advantage of a static inner class is that it doesn't need an instance of the containing class to work and it's bytecode class size is smaller for that reason - less overhead.


Can an inner class be creatd?

Yes. public class My { public class Wallet { } } //To create one My.Wallet billFolder = new My.Wallet();


Static keyword in java?

A Static method in Java is one that belongs to a class rather than an object of a class. Normal methods of a class can be invoked only by using an object of the class but a Static method can be invoked Directly. Example: public class A { ..... public static int getAge(){ .... } } public class B { ..... int age = A.getAge(); } In class B when we wanted the age value we directly called the method using the instance of the class instead of instantiating an object of the class. Tip: A static method can access only static variables. The reason is obvious. Something that is common to a class cannot refer to things that are specific to an object...


Inner class oops?

Inner class oops is a program. This program is smaller class within a bigger class.


What is static inner class?

An inner class declared as static is treated as if it were a normal top-level class.


What are inner classes?

Quite simply, an inner class is one class within another. Typically the inner class will be a private inner class, and will only be used by the outer class. class MyOuterClass { class MyInnerClass { } }


What is nested class?

A class declared as a member of another class is called as a nested class or a class with in class. the general syntax of the nested class is: class outer_class_name{ private: //data protected: //data public: //methods class inner_class_name{ private: //data of inner class public: //methods of inner class };//end of inner class };//end of outer class


Can interface have inner class?

Yes


When do you use inner classes?

You use inner classes when you know you'll never need to access that class from anywhere else. A common use of this is in a linked list implementation: public class LinkedList { private class LinkedListNode { } } There's no reason for any other class to have access to your node class, so it should be an inner class.