Advertisement
yo2man

Tutorial 42 Inner Clases

May 25th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. //Tutorial 42 Inner Clases
  2.  
  3.  
  4. //---------------------------------------------------------------------------------------------------------------------------------------
  5. //App.java
  6.  
  7. public class App {
  8.  
  9.     public static void main(String[] args) {
  10.        
  11.     Robot robot= new Robot(7);
  12.     robot.start();
  13.    
  14.     Robot.Battery battery = new Robot.Battery();
  15.     battery.charge();
  16.     }
  17.  
  18. }
  19. //---------------------------------------------------------------------------------------------------------------------------------------
  20. //Robot.java
  21.  
  22. public class Robot {
  23.    
  24.     private int id;
  25.     //Non Static Inner Class
  26.     private class Brain { //inner class
  27.         public void think(){
  28.             System.out.println("Robot " + id + " is thinking. ");
  29.         }
  30.     }
  31.     //Second Type of Inner Class: Static Inner Class
  32.    
  33.     /* static inner classes do not have access to instance data.
  34.        They are really just like "normal" classes, except that they are grouped
  35.        within an outer class. Use them for grouping classes together.
  36.      */
  37.     public static class Battery {
  38.         public void charge(){
  39.             System.out.println("Battery charging...");
  40.         }
  41.     }
  42.    
  43.     public Robot(int id) { //rightclick>source>generate constructor using id>delete "super();" because its not needed
  44.         this.id = id;
  45.     }
  46.    
  47.     public void start(){
  48.         System.out.println("Starting robot " + id);
  49.         // Use Brain. We don't have an instance of brain
  50.         // until we create one. Instances of brain are
  51.         // always associated with instances of Robot (the
  52.         // enclosing class).
  53.        
  54.         Brain brain = new Brain();
  55.         brain.think();
  56.    
  57.     // Sometimes it's useful to create local classes
  58.     // within methods. You can use them only within the method.
  59.        
  60.     final String name = "Robert";
  61.    
  62.     class Temp {
  63.             public void doSomething() {
  64.                 System.out.println("ID is: " + id);
  65.                 System.out.println("My name is " + name);
  66.             }
  67.         }
  68.     Temp temp = new Temp();
  69.     temp.doSomething();
  70.     }
  71. }
  72.  
  73. //---------------------------------------------------------------------------------------------------------------------------------------
  74. /* Run Results:
  75. Robot 7 is thinking.
  76. ID is: 7
  77. My name is Robert
  78. Battery charging...
  79. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement