0
class aa { public void bb() { class cc { public void dd() { System.out.println("hello"); } } } } 

How to call dd() method in main method?

class Solution { public static void main(String arg[]) { /* i want to call dd() here */ } } 
1
  • 1
    Through an instance of cc, as you do with any other class. Commented Nov 30, 2015 at 7:50

3 Answers 3

2

To call an instance method, you need an instance of that method e.g.

class aa { interface ii { public void dd(); } public ii bb() { // you can only call the method of a public interface or class // as cc implements ii, this allows you to call the method. class cc implements ii { public void dd() { System.out.println("hello"); } } return new cc(); } } 

later

new aa().bb().dd(); 
Sign up to request clarification or add additional context in comments.

Comments

0
class aa { public void bb() { } static class cc { void dd() { System.out.println("hello"); } } public static void main(String[] args) { cc c = new aa.cc(); c.dd(); } } 
  1. You inner class should be in class aa not in method of class aa
  2. And cc class should be static

Comments

0

you can call it using calling bb() call from main like,

public static void main(String... s){ new aa().bb() } 

And modify bb()like,

public void bb() { class cc{ public void dd() { System.out.println("hello"); } } new cc().dd(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.