Programming Tutorials

(追記) (追記ここまで)

How to use and access the inner class in java

By: Saravanan in Java Tutorials on 2009年10月03日 [フレーム]

In this Tutorial we are going to see how to use a named inner class, how to access inner class in Java.

//A named inner class is used.
// This is used to show that it can access non-local variables in the enclosing object.
public class InnerExample
 {
 static String msg= "Welcome";
 public static void main(String[] arg)
 {
 class InnerClass {
 public void doWork()
 {
 System.out.println(msg);
 }
 }
 InnerClass i = new InnerClass();
 i.doWork();
 }
}
// Access inner class from outside 
public class InnerExample2
 {
 public static void main(String[] args)
 {
 OuterClass outer = new OuterClass();
 outer.new InnerClass().welcome();
 }
}
class OuterClass
 {
 public class InnerClass
 {
 public void welcome()
 {
 System.out.println("Welcome from InnerClass()");
 }
 }
}



(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /