I try to access an inner class method from another inner class. Both inner classes are declared in the same outer class:
class OuterFoo{
class innerFoo1{
public void methodFoo1(){
System.out.println(" Hello, i am in the inner foo 1");
}
}
class innerFoo2{
public void methodFoo2(){
System.out.println(" Hello, i am in the inner foo 2");
}
}
}
Now, I would like to access methodFoo1 from methodFoo2.
Any help will be appreciated.
You need a reference to an instance of the other inner class. Like this:
public class OuterFoo {
private class InnerFoo1 {
private void helloFoo1 () {
System.out.println("foo1");
InnerFoo2 foo2 = new InnerFoo2();
foo2.helloFoo2();
}
}
private class InnerFoo2 {
private void helloFoo2 () {
System.out.println("foo2");
InnerFoo1 foo1 = new InnerFoo1();
foo1.helloFoo1();
}
}
}
Depending on what you want your real program should do (if you don't need to instantiate the class InnerFoo and only need a call to a static method), you may also make the class innerFoo2 and the method methodFoo2 static.
public class OuterFoo{
class innerFoo1{
public void methodFoo1(){
System.out.println(" Hello, i am in the inner foo 1");
OuterFoo.innerFoo2.methodFoo2();
}
}
static class innerFoo2{
public static void methodFoo2(){
System.out.println(" Hello, i am in the inner foo 2");
}
}
}
If your inner classes are instanciated into the OuterFoo
You can do it this way :
class OuterFoo{
final innerFoo1 if1 = new innerFoo1();
final innerFoo1 if2 = new innerFoo2();
class innerFoo1{
public void methodFoo1(){
System.out.println(" Hello, i am in the inner foo 1");
if2.methodFoo2();
}
}
class innerFoo2{
public void methodFoo2(){
System.out.println(" Hello, i am in the inner foo 2");
if1.methodFoo1();
}
}
}
Try this code
public class OuterFoo{
class innerFoo1{
public void methodFoo1(){
System.out.println(" Hello, i am in the inner foo 1");
}
}
class innerFoo2{
public void methodFoo2(){
System.out.println(" Hello, i am in the inner foo 2");
}
}
void displayInnerFoo1(){
innerFoo1 object1= new innerFoo1();
object1.methodFoo1();
}
void displayInnerFoo2(){
innerFoo2 object2= new innerFoo2();
object2.methodFoo2();
}
public static void main(String args[]){
OuterFoo objectParent= new OuterFoo();
objectParent.displayInnerFoo1();
objectParent.displayInnerFoo2();
}
}
Create the Object of inner class then simply use the dot operator to access it.
ClassName obj = new ClassName();
obj.MethodName();
Related
I want to call the Inner class method in outer class, but it isn't working. I also made a reference of Inner class object and try to call the method, but also invalid again
class Outer{
int a;
public void show() {
System.out.println("Show Method");
}
Inner obj=new Inner();
obj.display();
class Inner{
public void display() {
System.out.println("Display Method");
}
}
}
It is very unclear what you are trying to do, however, the syntactically smallest possible change you can make to get your code to compile (any maybe do what you want, although it is not clear to me what it is that you are trying to achieve), would be to move the method call into an instance initializer:
class Outer {
int a;
public void show() {
System.out.println("Show Method");
}
Inner obj = new Inner();
{ obj.display(); }
// ↑ ↑
class Inner {
public void display() {
System.out.println("Display Method");
}
}
}
Now, given a suitable program entry point:
class Test {
public static void main(String... args) {
new Outer();
}
}
This will print:
Display Method
I'm extremely new to Java and have been trying to use Nested class for the first time.
I have an outer class with 2 inner class and i'm trying to be able to have
the innerclass
class OuterClass {
...
class Person {
class Like {
}
**public static Map<Integer, Like> Likes;**
}
}
Is it possible in Java ?
In Golang that would look like this for example :
type Like struct {
}
type Person struct {
Name string
**Likes map[int]Like**
}
I might be going the wrong way and maybe a better OOP approch of doing that exist or is it possible this way ?
Yes this is possible and your code will be like
class Client{
class Outer{
class Phone{
class Like{
public void sayHello(){
System.out.println("Hello");
}
}
public static void method(Outer.Phone.Like g){
l.sayHello();
}
}
}
public static void main(String[] args){
Client q = new Client();
Client.Outer o = q.new Outer();
Client.Outer.Phone p = o.new Phone();
Client.Outer.Phone.Like l = p.new Like();
p.method(l); //call sayHello and print Hello
}
class A { //1st code starts here
private void display() {
System.out.println("A class");
}
}
class B extends A {
protected void display() {
System.out.println("B class");
}
}
class Test {
public static void main(String args[]) {
A obj = new B();
obj.display();
}
}
Output : Test.java:22: error: display() has private access in A
obj.display();
class Outer{ //2nd Code starts here
class Inner1{
private void m2() {
System.out.println("Inner1 class");
}
}
class Inner2 extends Inner1{
protected void m2() {
System.out.println("Inner2 class");
}
}
public static void main(String args[]) {
Outer o=new Outer();
Outer.Inner1 i=o.new Inner2();
i.m2();
}
}
Output : Inner1 class
Why compile time error in 1st code while output Inner1 class in 2nd code???
The code of the Outer class can access any member or method declared within the Outer class, regardless of the access level. However, the m2 method being called is the method of the base class Inner1, since you can't override a private method.
On the other hand, the code of the Test class cannot access a private method of a different class, which is why that code doesn't pass compilation.
Because private members are accessible only in class.
when class B extends A private members are inaccessible For B.
In case of Inner classes, An inner class is a member of class and have access to all members of enclosing class.
How to access the public function of private static inner class in some other class Suppose there is a class structure like below :-
public class Outer{
private static Inner {
public void func() {
}
}
}
And there is another class :-
class UseFunc {
// I have to use the func() here
}
If I use like this it will give error : - create object of Inner like Outer.Inner oi = new Outer.Inner();
access oi.func() //as Inner is private class
Okay, this is a very bad (I mean, really terrible) implementation but it works:
public class Outer
{
private static class Inner
{
public void func()
{
}
}
public void encapsulatedFunc()
{
new Inner().func();
}
}
class UseFunc
{
public static void main(String[] args)
{
new Outer().encapsulatedFunc();
}
}
I can only imagine that code being used for educational purpose as a "What not to do" example.
I have the following code with a nested class called out1
class sample{
public int a=5;
class out1{
void main1(){
System.out.println("this is out1");
}
}
void call(){
//access main1() method on class out1
}
}
public class innerclass{
public static void main(String args[]){
sample ob=new sample();
System.out.println(ob.a);// access field a on class sample
//access call() on class sample
}
}
does anyone know on how to access inner class out1 and is it possible to access this inner class without using call() method on class sample?
You can create inner class out1 object as
ob.new out1();
This is my way how to access inner classes. I made a get method in class sample which returns an object of class out1:
public class innerclass {
public static void main(String args[]) {
sample ob = new sample();
ob.getOut1().call(); // calling the call() method in innerclass out1
}
}
class sample {
public int a = 5;
out1 getOut1() {
return new out1();
}
class out1 {
public void main1() {
System.out.println("this is out1");
}
public void call() {
main1();
}
}
}
And try to make classes with uppercase letter and also use camelcase like: Sample, InnerClass, Out1.
You can access the innerclass by new of outer.new of inner.
To call inner class method from outer class you need to create an object of the inner class.Otherwise you have to make the inner class as well as the method static.
class Sample{
public int a=5;
class Out1{
void main1(){
System.out.println("this is out1");
}
}
void call(){
new Out1().main1();
}
}
public class Innerclass{
public static void main(String args[]){
Sample ob=new Sample();
System.out.println(ob.a);// access field a on class sample
Sample.Out1 out1=new Sample().new Out1();
Out1.main1();
ob.call();
//access call() on class sample
}
}
And class names should start with capital letter by convention.
Inner classes can be static.
If you do not define your inner class as static, then you have to create an instance of it (an object) in order to use it.
Here is an example use case of static and member (non static) classes:
public class Tester {
public static void main() {
Outer outerTest = new Outer();
outerTest.test();
outerTest.publicInnerInstance.sayHello();
Outer.InnerStaticClass.sayHello();
}
}
class Outer{
class InnerMemberClass{
public void sayHello(){
System.out.println("Hello");
System.out.println("I'm an instance of 'InnerMemberClass'.");
}
}
static class InnerStaticClass{
public static void sayHello(){
System.out.println("Hello.");
System.out.println("I'm a static class 'InnerStaticClass'.");
}
}
public InnerMemberClass publicInnerInstance;
//'Outer' constructor
public void Outer(){
publicInnerInstance = new InnerMemberClass();
}
public void test(){
InnerStaticClass.sayHello();
InnerMemberClass instance = new InnerMemberClass();
instance.sayHello();
}
}